!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("@firebase/app-compat"),require("@firebase/app")):"function"==typeof define&&define.amd?define(["@firebase/app-compat","@firebase/app"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).firebase,e.firebase.INTERNAL.modularAPIs)}(this,function(wo,Co){"use strict";try{!(function(){function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n,t=e(wo);function r(t){const n=[];let r=0;for(let i=0;i>6|192:(55296==(64512&e)&&i+1>18|240,n[r++]=e>>12&63|128):n[r++]=e>>12|224,n[r++]=e>>6&63|128),n[r++]=63&e|128)}return n}const i={NODE_CLIENT:!1,NODE_ADMIN:!1,SDK_VERSION:"${JSCORE_VERSION}"},p=function(e,t){if(!e)throw c(t)},c=function(e){return new Error("Firebase Database ("+i.SDK_VERSION+") INTERNAL ASSERT FAILED: "+e)},s={byteToCharMap_:null,charToByteMap_:null,byteToCharMapWebSafe_:null,charToByteMapWebSafe_:null,ENCODED_VALS_BASE:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",get ENCODED_VALS(){return this.ENCODED_VALS_BASE+"+/="},get ENCODED_VALS_WEBSAFE(){return this.ENCODED_VALS_BASE+"-_."},HAS_NATIVE_SUPPORT:"function"==typeof atob,encodeByteArray(n,e){if(!Array.isArray(n))throw Error("encodeByteArray takes an array as a parameter");this.init_();var r=e?this.byteToCharMapWebSafe_:this.byteToCharMap_;const i=[];for(let c=0;c>6,t=63&h;l||(t=64,o||(e=64)),i.push(r[s>>2],r[(3&s)<<4|a>>4],r[e],r[t])}return i.join("")},encodeString(e,t){return this.HAS_NATIVE_SUPPORT&&!t?btoa(e):this.encodeByteArray(r(e),t)},decodeString(e,t){return this.HAS_NATIVE_SUPPORT&&!t?atob(e):function(e){const t=[];let n=0,r=0;for(;n>10)),t[r++]=String.fromCharCode(56320+(1023&s))):(i=e[n++],s=e[n++],t[r++]=String.fromCharCode((15&o)<<12|(63&i)<<6|63&s))}return t.join("")}(this.decodeStringToByteArray(e,t))},decodeStringToByteArray(e,t){this.init_();var n=t?this.charToByteMapWebSafe_:this.charToByteMap_;const r=[];for(let l=0;l>4),64!==o&&(r.push(s<<4&240|o>>2),64!==a&&r.push(o<<6&192|a))}return r},init_(){if(!this.byteToCharMap_){this.byteToCharMap_={},this.charToByteMap_={},this.byteToCharMapWebSafe_={},this.charToByteMapWebSafe_={};for(let e=0;e=this.ENCODED_VALS_BASE.length&&(this.charToByteMap_[this.ENCODED_VALS_WEBSAFE.charAt(e)]=e,this.charToByteMapWebSafe_[this.ENCODED_VALS.charAt(e)]=e)}}};class h extends Error{constructor(){super(...arguments),this.name="DecodeBase64StringError"}}function o(e){var t=r(e);return s.encodeByteArray(t,!0)}const a=function(e){return o(e).replace(/\./g,"")},l=function(e){try{return s.decodeString(e,!0)}catch(e){console.error("base64Decode failed: ",e)}return null};function u(e){return function e(t,n){if(!(n instanceof Object))return n;switch(n.constructor){case Date:const r=n;return new Date(r.getTime());case Object:void 0===t&&(t={});break;case Array:t=[];break;default:return n}for(const i in n)n.hasOwnProperty(i)&&d(i)&&(t[i]=e(t[i],n[i]));return t}(void 0,e)}function d(e){return"__proto__"!==e}class _{constructor(){this.reject=()=>{},this.resolve=()=>{},this.promise=new Promise((e,t)=>{this.resolve=e,this.reject=t})}wrapCallback(n){return(e,t)=>{e?this.reject(e):this.resolve(t),"function"==typeof n&&(this.promise.catch(()=>{}),1===n.length?n(e):n(e,t))}}}function f(){return"undefined"!=typeof window&&(window.cordova||window.phonegap||window.PhoneGap)&&/ios|iphone|ipod|ipad|android|blackberry|iemobile/i.test("undefined"!=typeof navigator&&"string"==typeof navigator.userAgent?navigator.userAgent:"")}function g(){return!0===i.NODE_ADMIN}function m(e){return JSON.parse(e)}function v(e){return JSON.stringify(e)}function y(e){let t={},n={},r={},i="";try{var s=e.split(".");t=m(l(s[0])||""),n=m(l(s[1])||""),i=s[2],r=n.d||{},delete n.d}catch(e){}return{header:t,claims:n,data:r,signature:i}}function w(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function C(e,t){if(Object.prototype.hasOwnProperty.call(e,t))return e[t]}function b(e){for(const t in e)if(Object.prototype.hasOwnProperty.call(e,t))return!1;return!0}function I(e,t,n){const r={};for(const i in e)Object.prototype.hasOwnProperty.call(e,i)&&(r[i]=t.call(n,e[i],i,e));return r}class T{constructor(){this.chain_=[],this.buf_=[],this.W_=[],this.pad_=[],this.inbuf_=0,this.total_=0,this.blockSize=64,this.pad_[0]=128;for(let e=1;e>>31)}let t=this.chain_[0],s=this.chain_[1],o=this.chain_[2],a=this.chain_[3],l=this.chain_[4],h,c;for(let _=0;_<80;_++){c=_<40?_<20?(h=a^s&(o^a),1518500249):(h=s^o^a,1859775393):_<60?(h=s&o|a&(s|o),2400959708):(h=s^o^a,3395469782);var u=(t<<5|t>>>27)+h+l+c+i[_]&4294967295;l=a,a=o,o=4294967295&(s<<30|s>>>2),s=t,t=u}this.chain_[0]=this.chain_[0]+t&4294967295,this.chain_[1]=this.chain_[1]+s&4294967295,this.chain_[2]=this.chain_[2]+o&4294967295,this.chain_[3]=this.chain_[3]+a&4294967295,this.chain_[4]=this.chain_[4]+l&4294967295}update(n,r){if(null!=n){var i=(r=void 0===r?n.length:r)-this.blockSize;let e=0;const s=this.buf_;let t=this.inbuf_;for(;e>e&255,++n;return t}}function E(e,t,n,r){let i;if(r"INTERNAL"in e).map(e=>e.INTERNAL.delete()),...e.filter(e=>"_delete"in e).map(e=>e._delete())])}isComponentSet(){return null!=this.component}isInitialized(e=D){return this.instances.has(e)}getOptions(e=D){return this.instancesOptions.get(e)||{}}initialize(e={}){var{options:t={}}=e,n=this.normalizeInstanceIdentifier(e.instanceIdentifier);if(this.isInitialized(n))throw Error(`${this.name}(${n}) has already been initialized`);if(!this.isComponentSet())throw Error(`Component ${this.name} has not been registered yet`);var r,i,s=this.getOrInitializeService({instanceIdentifier:n,options:t});for([r,i]of this.instancesDeferred.entries())n===this.normalizeInstanceIdentifier(r)&&i.resolve(s);return s}onInit(e,t){var n=this.normalizeInstanceIdentifier(t);const r=null!==(i=this.onInitCallbacks.get(n))&&void 0!==i?i:new Set;r.add(e),this.onInitCallbacks.set(n,r);var i=this.instances.get(n);return i&&e(i,n),()=>{r.delete(e)}}invokeOnInitCallbacks(e,t){var n=this.onInitCallbacks.get(t);if(n)for(const r of n)try{r(e,t)}catch(e){}}getOrInitializeService({instanceIdentifier:e,options:t={}}){let n=this.instances.get(e);if(!n&&this.component&&(n=this.component.instanceFactory(this.container,{instanceIdentifier:(r=e)===D?void 0:r,options:t}),this.instances.set(e,n),this.instancesOptions.set(e,t),this.invokeOnInitCallbacks(n,e),this.component.onInstanceCreated))try{this.component.onInstanceCreated(this.container,e,n)}catch(e){}var r;return n||null}normalizeInstanceIdentifier(e=D){return!this.component||this.component.multipleInstances?e:D}shouldAutoInitialize(){return!!this.component&&"EXPLICIT"!==this.component.instantiationMode}}class O{constructor(e){this.name=e,this.providers=new Map}addComponent(e){const t=this.getProvider(e.name);if(t.isComponentSet())throw new Error(`Component ${e.name} has already been registered with ${this.name}`);t.setComponent(e)}addOrOverwriteComponent(e){const t=this.getProvider(e.name);t.isComponentSet()&&this.providers.delete(e.name),this.addComponent(e)}getProvider(e){if(this.providers.has(e))return this.providers.get(e);var t=new A(e,this);return this.providers.set(e,t),t}getProviders(){return Array.from(this.providers.values())}}($=n=n||{})[$.DEBUG=0]="DEBUG",$[$.VERBOSE=1]="VERBOSE",$[$.INFO=2]="INFO",$[$.WARN=3]="WARN",$[$.ERROR=4]="ERROR",$[$.SILENT=5]="SILENT";const L={debug:n.DEBUG,verbose:n.VERBOSE,info:n.INFO,warn:n.WARN,error:n.ERROR,silent:n.SILENT},M=n.INFO,F={[n.DEBUG]:"log",[n.VERBOSE]:"log",[n.INFO]:"info",[n.WARN]:"warn",[n.ERROR]:"error"},q=(e,t,...n)=>{if(!(t>6|192:(e<65536?n[r++]=e>>12|224:(n[r++]=e>>18|240,n[r++]=e>>12&63|128),n[r++]=e>>6&63|128),n[r++]=63&e|128)}return n}(e);const n=new T;return n.update(t),t=n.digest(),s.encodeByteArray(t)}const ee=H("localStorage"),te=H("sessionStorage"),ne=new W("@firebase/database"),re=function(){let e=1;return function(){return e++}}(),ie=function(...e){let t="";for(let r=0;rn?r.push(e.substring(i,n)):r.push(e.substring(i,i+t));return r}const _e=function(e,t){p(!t||!0===e||!1===e,"Can't turn on custom loggers persistently."),!0===e?(ne.logLevel=n.VERBOSE,se=ne.log.bind(ne),t&&te.set("logging_enabled",!0)):"function"==typeof e?se=e:(se=null,te.remove("logging_enabled"))},pe=function(...e){var t;!0===oe&&(oe=!1,null===se&&!0===te.get("logging_enabled")&&_e(!0)),se&&(t=ie.apply(null,e),se(t))},fe=function(...e){var t=`FIREBASE FATAL ERROR: ${ie(...e)}`;throw ne.error(t),new Error(t)},ge=function(...e){var t="FIREBASE WARNING: "+ie(...e);ne.warn(t)},me=function(){"undefined"!=typeof window&&window.location&&window.location.protocol&&-1!==window.location.protocol.indexOf("https:")&&ge("Insecure Firebase access from a secure page. Please use https in calls to new Firebase().")},ve=function(e){return"number"==typeof e&&(e!=e||e===Number.POSITIVE_INFINITY||e===Number.NEGATIVE_INFINITY)},ye="[MIN_NAME]",we="[MAX_NAME]",Ce=function(e,t){if(e===t)return 0;if(e===ye||t===we)return-1;if(t===ye||e===we)return 1;var n=Te(e),r=Te(t);return null!==n?null!==r?n-r==0?e.length-t.length:n-r:-1:null===r&&e=Math.pow(2,-1022)?(i=Math.min(Math.floor(Math.log(e)/Math.LN2),1023),n=i+1023,Math.round(e*Math.pow(2,52-i)-Math.pow(2,52))):(n=0,Math.round(e/Math.pow(2,-1074))));const o=[];for(s=52;s;--s)o.push(r%2?1:0),r=Math.floor(r/2);for(s=11;s;--s)o.push(n%2?1:0),n=Math.floor(n/2);o.push(t?1:0),o.reverse();const a=o.join("");let l="";for(s=0;s<64;s+=8){let e=parseInt(a.substr(s,8),2).toString(16);1===e.length&&(e="0"+e),l+=e}return l.toLowerCase()}function Te(e){if(Se.test(e)){var t=Number(e);if(t>=ke&&t<=Ne)return t}return null}function Ee(e,t){const n=setTimeout(e,t);return"number"==typeof n&&"undefined"!=typeof Deno&&Deno.unrefTimer?Deno.unrefTimer(n):"object"==typeof n&&n.unref&&n.unref(),n}const Se=new RegExp("^-?(0*)\\d{1,10}$"),ke=-2147483648,Ne=2147483647,Pe=function(e){try{e()}catch(t){setTimeout(()=>{var e=t.stack||"";throw ge("Exception was thrown by user callback.",e),t},Math.floor(0))}};class Re{constructor(e,t){this.appName_=e,this.appCheckProvider=t,this.appCheck=null==t?void 0:t.getImmediate({optional:!0}),this.appCheck||null!=t&&t.get().then(e=>this.appCheck=e)}getToken(n){return this.appCheck?this.appCheck.getToken(n):new Promise((e,t)=>{setTimeout(()=>{this.appCheck?this.getToken(n).then(e,t):e(null)},0)})}addTokenChangeListener(t){var e;null!==(e=this.appCheckProvider)&&void 0!==e&&e.get().then(e=>e.addTokenListener(t))}notifyForInvalidToken(){ge(`Provided AppCheck credentials for the app named "${this.appName_}" `+"are invalid. This usually indicates your app was not initialized correctly.")}}class xe{constructor(e,t,n){this.appName_=e,this.firebaseOptions_=t,this.authProvider_=n,this.auth_=null,this.auth_=n.getImmediate({optional:!0}),this.auth_||n.onInit(e=>this.auth_=e)}getToken(n){return this.auth_?this.auth_.getToken(n).catch(e=>e&&"auth/token-not-initialized"===e.code?(pe("Got auth/token-not-initialized error. Treating as null token."),null):Promise.reject(e)):new Promise((e,t)=>{setTimeout(()=>{this.auth_?this.getToken(n).then(e,t):e(null)},0)})}addTokenChangeListener(t){this.auth_?this.auth_.addAuthTokenListener(t):this.authProvider_.get().then(e=>e.addAuthTokenListener(t))}removeTokenChangeListener(t){this.authProvider_.get().then(e=>e.removeAuthTokenListener(t))}notifyForInvalidToken(){let e='Provided authentication credentials for the app named "'+this.appName_+'" are invalid. This usually indicates your app was not initialized correctly. ';"credential"in this.firebaseOptions_?e+='Make sure the "credential" property provided to initializeApp() is authorized to access the specified "databaseURL" and is from the correct project.':"serviceAccount"in this.firebaseOptions_?e+='Make sure the "serviceAccount" property provided to initializeApp() is authorized to access the specified "databaseURL" and is from the correct project.':e+='Make sure the "apiKey" and "databaseURL" properties provided to initializeApp() match the values provided for your app at https://console.firebase.google.com/.',ge(e)}}class De{constructor(e){this.accessToken=e}getToken(e){return Promise.resolve({accessToken:this.accessToken})}addTokenChangeListener(e){e(this.accessToken)}removeTokenChangeListener(e){}notifyForInvalidToken(){}}De.OWNER="owner";const Ae=/(console\.firebase|firebase-console-\w+\.corp|firebase\.corp)\.google\.com/,Oe="websocket",Le="long_polling";class Me{constructor(e,t,n,r,i=!1,s="",o=!1,a=!1){this.secure=t,this.namespace=n,this.webSocketOnly=r,this.nodeAdmin=i,this.persistenceKey=s,this.includeNamespaceInQueryParams=o,this.isUsingEmulator=a,this._host=e.toLowerCase(),this._domain=this._host.substr(this._host.indexOf(".")+1),this.internalHost=ee.get("host:"+e)||this._host}isCacheableHost(){return"s-"===this.internalHost.substr(0,2)}isCustomHost(){return"firebaseio.com"!==this._domain&&"firebaseio-demo.com"!==this._domain}get host(){return this._host}set host(e){e!==this.internalHost&&(this.internalHost=e,this.isCacheableHost()&&ee.set("host:"+this._host,this.internalHost))}toString(){let e=this.toURLString();return this.persistenceKey&&(e+="<"+this.persistenceKey+">"),e}toURLString(){var e=this.secure?"https://":"http://",t=this.includeNamespaceInQueryParams?`?ns=${this.namespace}`:"";return`${e}${this.host}/${t}`}}function Fe(e,t,n){p("string"==typeof t,"typeof type must == string"),p("object"==typeof n,"typeof params must == object");let r;if(t===Oe)r=(e.secure?"wss://":"ws://")+e.internalHost+"/.ws?";else{if(t!==Le)throw new Error("Unknown connection type: "+t);r=(e.secure?"https://":"http://")+e.internalHost+"/.lp?"}((t=e).host!==t.internalHost||t.isCustomHost()||t.includeNamespaceInQueryParams)&&(n.ns=e.namespace);const i=[];return be(n,(e,t)=>{i.push(e+"="+t)}),r+i.join("&")}class qe{constructor(){this.counters_={}}incrementCounter(e,t=1){w(this.counters_,e)||(this.counters_[e]=0),this.counters_[e]+=t}get(){return u(this.counters_)}}const We={},Ue={};function Be(e){var t=e.toString();return We[t]||(We[t]=new qe),We[t]}class je{constructor(e){this.onMessage_=e,this.pendingResponses=[],this.currentResponseNum=0,this.closeAfterResponse=-1,this.onClose=null}closeAfter(e,t){this.closeAfterResponse=e,this.onClose=t,this.closeAfterResponse{this.onMessage_(n[e])});if(this.currentResponseNum===this.closeAfterResponse){this.onClose&&(this.onClose(),this.onClose=null);break}this.currentResponseNum++}}}class Ve{constructor(e,t,n,r,i,s,o){this.connId=e,this.repoInfo=t,this.applicationId=n,this.appCheckToken=r,this.authToken=i,this.transportSessionId=s,this.lastSessionId=o,this.bytesSent=0,this.bytesReceived=0,this.everConnected_=!1,this.log_=ae(e),this.stats_=Be(t),this.urlFn=e=>(this.appCheckToken&&(e.ac=this.appCheckToken),Fe(t,Le,e))}open(e,t){this.curSegmentNum=0,this.onDisconnect_=t,this.myPacketOrderer=new je(e),this.isClosed_=!1,this.connectTimeoutTimer_=setTimeout(()=>{this.log_("Timed out trying to connect."),this.onClosed_(),this.connectTimeoutTimer_=null},Math.floor(3e4)),function(t){if("complete"===document.readyState)t();else{let e=!1;const n=function(){document.body?e||(e=!0,t()):setTimeout(n,Math.floor(10))};document.addEventListener?(document.addEventListener("DOMContentLoaded",n,!1),window.addEventListener("load",n,!1)):document.attachEvent&&(document.attachEvent("onreadystatechange",()=>{"complete"===document.readyState&&n()}),window.attachEvent("onload",n))}}(()=>{if(!this.isClosed_){this.scriptTagHolder=new ze((...e)=>{var[t,n,r]=e;if(this.incrementIncomingBytes_(e),this.scriptTagHolder)if(this.connectTimeoutTimer_&&(clearTimeout(this.connectTimeoutTimer_),this.connectTimeoutTimer_=null),this.everConnected_=!0,"start"===t)this.id=n,this.password=r;else{if("close"!==t)throw new Error("Unrecognized command received: "+t);n?(this.scriptTagHolder.sendNewPolls=!1,this.myPacketOrderer.closeAfter(n,()=>{this.onClosed_()})):this.onClosed_()}},(...e)=>{var[t,n]=e;this.incrementIncomingBytes_(e),this.myPacketOrderer.handleResponse(t,n)},()=>{this.onClosed_()},this.urlFn);const t={start:"t"};t.ser=Math.floor(1e8*Math.random()),this.scriptTagHolder.uniqueCallbackIdentifier&&(t.cb=this.scriptTagHolder.uniqueCallbackIdentifier),t.v="5",this.transportSessionId&&(t.s=this.transportSessionId),this.lastSessionId&&(t.ls=this.lastSessionId),this.applicationId&&(t.p=this.applicationId),this.appCheckToken&&(t.ac=this.appCheckToken),"undefined"!=typeof location&&location.hostname&&Ae.test(location.hostname)&&(t.r="f");var e=this.urlFn(t);this.log_("Connecting via long-poll to "+e),this.scriptTagHolder.addTag(e,()=>{})}})}start(){this.scriptTagHolder.startLongPoll(this.id,this.password),this.addDisconnectPingFrame(this.id,this.password)}static forceAllow(){Ve.forceAllow_=!0}static forceDisallow(){Ve.forceDisallow_=!0}static isAvailable(){return!!Ve.forceAllow_||!(Ve.forceDisallow_||"undefined"==typeof document||null==document.createElement||"object"==typeof window&&window.chrome&&window.chrome.extension&&!/^chrome/.test(window.location.href)||"object"==typeof Windows&&"object"==typeof Windows.UI)}markConnectionHealthy(){}shutdown_(){this.isClosed_=!0,this.scriptTagHolder&&(this.scriptTagHolder.close(),this.scriptTagHolder=null),this.myDisconnFrame&&(document.body.removeChild(this.myDisconnFrame),this.myDisconnFrame=null),this.connectTimeoutTimer_&&(clearTimeout(this.connectTimeoutTimer_),this.connectTimeoutTimer_=null)}onClosed_(){this.isClosed_||(this.log_("Longpoll is closing itself"),this.shutdown_(),this.onDisconnect_&&(this.onDisconnect_(this.everConnected_),this.onDisconnect_=null))}close(){this.isClosed_||(this.log_("Longpoll is being closed."),this.shutdown_())}send(e){var t=v(e);this.bytesSent+=t.length,this.stats_.incrementCounter("bytes_sent",t.length);var t=o(t),n=de(t,1840);for(let r=0;rdocument.domain="'+i+'";<\/script>');var i=""+e+"";try{this.myIFrame.doc.open(),this.myIFrame.doc.write(i),this.myIFrame.doc.close()}catch(e){pe("frame writing exception"),e.stack&&pe(e.stack),pe(e)}}}static createIFrame_(){const t=document.createElement("iframe");if(t.style.display="none",!document.body)throw"Document body has not initialized. Wait to initialize Firebase until after the document is ready.";document.body.appendChild(t);try{t.contentWindow.document||pe("No IE domain setting required")}catch(e){var n=document.domain;t.src="javascript:void((function(){document.open();document.domain='"+n+"';document.close();})())"}return t.contentDocument?t.doc=t.contentDocument:t.contentWindow?t.doc=t.contentWindow.document:t.document&&(t.doc=t.document),t}close(){this.alive=!1,this.myIFrame&&(this.myIFrame.doc.body.textContent="",setTimeout(()=>{null!==this.myIFrame&&(document.body.removeChild(this.myIFrame),this.myIFrame=null)},Math.floor(0)));const e=this.onDisconnect;e&&(this.onDisconnect=null,e())}startLongPoll(e,t){for(this.myID=e,this.myPW=t,this.alive=!0;this.newRequest_(););}newRequest_(){if(this.alive&&this.sendNewPolls&&this.outstandingRequests.size<(0{this.outstandingRequests.delete(t),this.newRequest_()},r=setTimeout(n,Math.floor(25e3));this.addTag(e,()=>{clearTimeout(r),n()})}addTag(e,n){setTimeout(()=>{try{if(!this.sendNewPolls)return;const t=this.myIFrame.doc.createElement("script");t.type="text/javascript",t.async=!0,t.src=e,t.onload=t.onreadystatechange=function(){var e=t.readyState;e&&"loaded"!==e&&"complete"!==e||(t.onload=t.onreadystatechange=null,t.parentNode&&t.parentNode.removeChild(t),n())},t.onerror=()=>{pe("Long-poll script failed to load: "+e),this.sendNewPolls=!1,this.close()},this.myIFrame.doc.body.appendChild(t)}catch(e){}},Math.floor(1))}}let He=null;"undefined"!=typeof MozWebSocket?He=MozWebSocket:"undefined"!=typeof WebSocket&&(He=WebSocket);class Qe{constructor(e,t,n,r,i,s,o){this.connId=e,this.applicationId=n,this.appCheckToken=r,this.authToken=i,this.keepaliveTimer=null,this.frames=null,this.totalFrames=0,this.bytesSent=0,this.bytesReceived=0,this.log_=ae(this.connId),this.stats_=Be(t),this.connURL=Qe.connectionURL_(t,s,o,r,n),this.nodeAdmin=t.nodeAdmin}static connectionURL_(e,t,n,r,i){const s={v:"5"};return"undefined"!=typeof location&&location.hostname&&Ae.test(location.hostname)&&(s.r="f"),t&&(s.s=t),n&&(s.ls=n),r&&(s.ac=r),i&&(s.p=i),Fe(e,Oe,s)}open(e,t){this.onDisconnect=t,this.onMessage=e,this.log_("Websocket connecting to "+this.connURL),this.everConnected_=!1,ee.set("previous_websocket_failure",!0);try{g(),this.mySock=new He(this.connURL,[],void 0)}catch(e){this.log_("Error instantiating WebSocket.");var n=e.message||e.data;return n&&this.log_(n),void this.onClosed_()}this.mySock.onopen=()=>{this.log_("Websocket connected."),this.everConnected_=!0},this.mySock.onclose=()=>{this.log_("Websocket connection was disconnected."),this.mySock=null,this.onClosed_()},this.mySock.onmessage=e=>{this.handleIncomingFrame(e)},this.mySock.onerror=e=>{this.log_("WebSocket error. Closing connection.");var t=e.message||e.data;t&&this.log_(t),this.onClosed_()}}start(){}static forceDisallow(){Qe.forceDisallow_=!0}static isAvailable(){let e=!1;var t;return"undefined"==typeof navigator||!navigator.userAgent||(t=navigator.userAgent.match(/Android ([0-9]{0,}\.[0-9]{0,})/))&&1{this.mySock&&this.sendString_("0"),this.resetKeepAlive()},Math.floor(45e3))}sendString_(e){try{this.mySock.send(e)}catch(e){this.log_("Exception thrown from WebSocket.send():",e.message||e.data,"Closing connection."),setTimeout(this.onClosed_.bind(this),0)}}}Qe.responsesRequiredToBeHealthy=2,Qe.healthyTimeout=3e4;class Ye{constructor(e){this.initTransports_(e)}static get ALL_TRANSPORTS(){return[Ve,Qe]}static get IS_TRANSPORT_INITIALIZED(){return this.globalTransportInitialized_}initTransports_(e){var t=Qe&&Qe.isAvailable();let n=t&&!Qe.previouslyFailed();if(e.webSocketOnly&&(t||ge("wss:// URL used, but browser isn't known to support websockets. Trying anyway."),n=!0),n)this.transports_=[Qe];else{const r=this.transports_=[];for(const i of Ye.ALL_TRANSPORTS)i&&i.isAvailable()&&r.push(i);Ye.globalTransportInitialized_=!0}}initialTransport(){if(0{this.conn_&&this.conn_.open(t,n)},Math.floor(0));var r=e.healthyTimeout||0;0{this.healthyTimeout_=null,this.isHealthy_||(this.conn_&&102400{t===this.conn_?this.onConnectionLost_(e):t===this.secondaryConn_?(this.log_("Secondary connection lost."),this.onSecondaryConnectionLost_()):this.log_("closing an old connection")}}connReceiver_(t){return e=>{2!==this.state_&&(t===this.rx_?this.onPrimaryMessageReceived_(e):t===this.secondaryConn_?this.onSecondaryMessageReceived_(e):this.log_("message on old connection"))}}sendRequest(e){this.sendData_({t:"d",d:e})}tryCleanupConnection(){this.tx_===this.secondaryConn_&&this.rx_===this.secondaryConn_&&(this.log_("cleaning up and promoting a connection: "+this.secondaryConn_.connId),this.conn_=this.secondaryConn_,this.secondaryConn_=null)}onSecondaryControl_(e){var t;"t"in e&&("a"===(t=e.t)?this.upgradeIfSecondaryHealthy_():"r"===t?(this.log_("Got a reset on secondary, closing it"),this.secondaryConn_.close(),this.tx_!==this.secondaryConn_&&this.rx_!==this.secondaryConn_||this.close()):"o"===t&&(this.log_("got pong on secondary."),this.secondaryResponsesRequired_--,this.upgradeIfSecondaryHealthy_()))}onSecondaryMessageReceived_(e){var t=ce("t",e),n=ce("d",e);if("c"===t)this.onSecondaryControl_(n);else{if("d"!==t)throw new Error("Unknown protocol layer: "+t);this.pendingDataMessages.push(n)}}upgradeIfSecondaryHealthy_(){this.secondaryResponsesRequired_<=0?(this.log_("Secondary connection is healthy."),this.isHealthy_=!0,this.secondaryConn_.markConnectionHealthy(),this.proceedWithUpgrade_()):(this.log_("sending ping on secondary."),this.secondaryConn_.send({t:"c",d:{t:"p",d:{}}}))}proceedWithUpgrade_(){this.secondaryConn_.start(),this.log_("sending client ack on secondary"),this.secondaryConn_.send({t:"c",d:{t:"a",d:{}}}),this.log_("Ending transmission on primary"),this.conn_.send({t:"c",d:{t:"n",d:{}}}),this.tx_=this.secondaryConn_,this.tryCleanupConnection()}onPrimaryMessageReceived_(e){var t=ce("t",e),n=ce("d",e);"c"===t?this.onControl_(n):"d"===t&&this.onDataMessage_(n)}onDataMessage_(e){this.onPrimaryResponse_(),this.onMessage_(e)}onPrimaryResponse_(){this.isHealthy_||(this.primaryResponsesRequired_--,this.primaryResponsesRequired_<=0&&(this.log_("Primary connection is healthy."),this.isHealthy_=!0,this.conn_.markConnectionHealthy()))}onControl_(e){var t=ce("t",e);if("d"in e){var n=e.d;if("h"===t){const r=Object.assign({},n);this.repoInfo_.isUsingEmulator&&(r.h=this.repoInfo_.host),this.onHandshake_(r)}else if("n"===t){this.log_("recvd end transmission on primary"),this.rx_=this.secondaryConn_;for(let e=0;e{this.secondaryConn_&&(this.log_("Timed out trying to upgrade."),this.secondaryConn_.close())},Math.floor(6e4))}onReset_(e){this.log_("Reset packet received. New host: "+e),this.repoInfo_.host=e,1===this.state_?this.close():(this.closeConnections_(),this.start_())}onConnectionEstablished_(e,t){this.log_("Realtime connection established."),this.conn_=e,this.state_=1,this.onReady_&&(this.onReady_(t,this.sessionId),this.onReady_=null),0===this.primaryResponsesRequired_?(this.log_("Primary connection is healthy."),this.isHealthy_=!0):Ee(()=>{this.sendPingOnPrimaryIfNecessary_()},Math.floor(5e3))}sendPingOnPrimaryIfNecessary_(){this.isHealthy_||1!==this.state_||(this.log_("sending ping on primary."),this.sendData_({t:"c",d:{t:"p",d:{}}}))}onSecondaryConnectionLost_(){var e=this.secondaryConn_;this.secondaryConn_=null,this.tx_!==e&&this.rx_!==e||this.close()}onConnectionLost_(e){this.conn_=null,e||0!==this.state_?1===this.state_&&this.log_("Realtime connection lost."):(this.log_("Realtime connection failed."),this.repoInfo_.isCacheableHost()&&(ee.remove("host:"+this.repoInfo_.host),this.repoInfo_.internalHost=this.repoInfo_.host)),this.close()}onConnectionShutdown_(e){this.log_("Connection shutdown command received. Shutting down..."),this.onKill_&&(this.onKill_(e),this.onKill_=null),this.onDisconnect_=null,this.close()}sendData_(e){if(1!==this.state_)throw"Connection is not connected";this.tx_.send(e)}close(){2!==this.state_&&(this.log_("Closing realtime connection."),this.state_=2,this.closeConnections_(),this.onDisconnect_&&(this.onDisconnect_(),this.onDisconnect_=null))}closeConnections_(){this.log_("Shutting down all connections"),this.conn_&&(this.conn_.close(),this.conn_=null),this.secondaryConn_&&(this.secondaryConn_.close(),this.secondaryConn_=null),this.healthyTimeout_&&(clearTimeout(this.healthyTimeout_),this.healthyTimeout_=null)}}class $e{put(e,t,n,r){}merge(e,t,n,r){}refreshAuthToken(e){}refreshAppCheckToken(e){}onDisconnectPut(e,t,n){}onDisconnectMerge(e,t,n){}onDisconnectCancel(e,t){}reportStats(e){}}class Ge{constructor(e){this.allowedEvents_=e,this.listeners_={},p(Array.isArray(e)&&0e===t),"Unknown event: "+t)}}class Je extends Ge{constructor(){super(["online"]),this.online_=!0,"undefined"==typeof window||void 0===window.addEventListener||f()||(window.addEventListener("online",()=>{this.online_||(this.online_=!0,this.trigger("online",!0))},!1),window.addEventListener("offline",()=>{this.online_&&(this.online_=!1,this.trigger("online",!1))},!1))}static getInstance(){return new Je}getInitialEvent(e){return p("online"===e,"Unknown event type: "+e),[this.online_]}currentlyOnline(){return this.online_}}class Xe{constructor(n,e){if(void 0===e){this.pieces_=n.split("/");let e=0;for(let t=0;t=e.pieces_.length?null:e.pieces_[e.pieceNum_]}function tt(e){return e.pieces_.length-e.pieceNum_}function nt(e){let t=e.pieceNum_;return t=e.pieces_.length)return null;const t=[];for(let n=e.pieceNum_;n=e.pieces_.length}function lt(e,t){var n=et(e),r=et(t);if(null===n)return t;if(n===r)return lt(nt(e),nt(t));throw new Error("INTERNAL ERROR: innerPath ("+t+") is not within outerPath ("+e+")")}function ht(e,t){var n=it(e,0),r=it(t,0);for(let s=0;stt(t))return!1;for(;n{var e=!document[t];e!==this.visible_&&(this.visible_=e,this.trigger("visible",e))},!1)}static getInstance(){return new ft}getInitialEvent(e){return p("visible"===e,"Unknown event type: "+e),[this.visible_]}}class gt extends $e{constructor(e,t,n,r,i,s,o,a){if(super(),this.repoInfo_=e,this.applicationId_=t,this.onDataUpdate_=n,this.onConnectStatus_=r,this.onServerInfoUpdate_=i,this.authTokenProvider_=s,this.appCheckTokenProvider_=o,this.authOverride_=a,this.id=gt.nextPersistentConnectionId_++,this.log_=ae("p:"+this.id+":"),this.interruptReasons_={},this.listens=new Map,this.outstandingPuts_=[],this.outstandingGets_=[],this.outstandingPutCount_=0,this.outstandingGetCount_=0,this.onDisconnectRequestQueue_=[],this.connected_=!1,this.reconnectDelay_=1e3,this.maxReconnectDelay_=3e5,this.securityDebugCallback_=null,this.lastSessionId=null,this.establishConnectionTimer_=null,this.visible_=!1,this.requestCBHash_={},this.requestNumber_=0,this.realtime_=null,this.authToken_=null,this.appCheckToken_=null,this.forceTokenRefresh_=!1,this.invalidAuthTokenCount_=0,this.invalidAppCheckTokenCount_=0,this.firstConnection_=!0,this.lastConnectionAttemptTime_=null,this.lastConnectionEstablishedTime_=null,a&&!g())throw new Error("Auth override specified in options, but not supported on non Node.js platforms");ft.getInstance().on("visible",this.onVisible_,this),-1===e.host.indexOf("fblocal")&&Je.getInstance().on("online",this.onOnline_,this)}sendRequest(e,t,n){var r=++this.requestNumber_,i={r:r,a:e,b:t};this.log_(v(i)),p(this.connected_,"sendRequest call when we're not connected not allowed."),this.realtime_.sendRequest(i),n&&(this.requestCBHash_[r]=n)}get(e){this.initConnection_();const n=new _;var t={p:e._path.toString(),q:e._queryObject};this.outstandingGets_.push({action:"g",request:t,onComplete:e=>{var t=e.d;"ok"===e.s?n.resolve(t):n.reject(t)}}),this.outstandingGetCount_++;t=this.outstandingGets_.length-1;return this.connected_&&this.sendGet_(t),n.promise}listen(e,t,n,r){this.initConnection_();var i=e._queryIdentifier,s=e._path.toString();this.log_("Listen called for "+s+" "+i),this.listens.has(s)||this.listens.set(s,new Map),p(e._queryParams.isDefault()||!e._queryParams.loadsAllData(),"listen() called for non-default but complete query"),p(!this.listens.get(s).has(i),"listen() called twice for same path/queryId.");var o={onComplete:r,hashFn:t,query:e,tag:n};this.listens.get(s).set(i,o),this.connected_&&this.sendListen_(o)}sendGet_(t){const n=this.outstandingGets_[t];this.sendRequest("g",n.request,e=>{delete this.outstandingGets_[t],this.outstandingGetCount_--,0===this.outstandingGetCount_&&(this.outstandingGets_=[]),n.onComplete&&n.onComplete(e)})}sendListen_(r){const i=r.query,s=i._path.toString(),o=i._queryIdentifier;this.log_("Listen on "+s+" for "+o);const e={p:s};r.tag&&(e.q=i._queryObject,e.t=r.tag),e.h=r.hashFn(),this.sendRequest("q",e,e=>{var t=e.d,n=e.s;gt.warnOnListenWarnings_(t,i),(this.listens.get(s)&&this.listens.get(s).get(o))===r&&(this.log_("listen response",e),"ok"!==n&&this.removeListen_(s,o),r.onComplete&&r.onComplete(n,t))})}static warnOnListenWarnings_(e,t){if(e&&"object"==typeof e&&w(e,"w")){const i=C(e,"w");var n,r;Array.isArray(i)&&~i.indexOf("no_index")&&(n='".indexOn": "'+t._queryParams.getIndex().toString()+'"',r=t._path.toString(),ge("Using an unspecified index. Your data will be downloaded and "+`filtered on the client. Consider adding ${n} at `+`${r} to your security rules for better performance.`))}}refreshAuthToken(e){this.authToken_=e,this.log_("Auth token refreshed"),this.authToken_?this.tryAuth():this.connected_&&this.sendRequest("unauth",{},()=>{}),this.reduceReconnectDelayIfAdminCredential_(e)}reduceReconnectDelayIfAdminCredential_(e){var t;(e&&40===e.length||(e=e,"object"==typeof(t=y(e).claims)&&!0===t.admin))&&(this.log_("Admin auth credential detected. Reducing max reconnect time."),this.maxReconnectDelay_=3e4)}refreshAppCheckToken(e){this.appCheckToken_=e,this.log_("App check token refreshed"),this.appCheckToken_?this.tryAppCheck():this.connected_&&this.sendRequest("unappeck",{},()=>{})}tryAuth(){if(this.connected_&&this.authToken_){const r=this.authToken_;var e=function(e){const t=y(e),n=t.claims;return!!n&&"object"==typeof n&&n.hasOwnProperty("iat")}(r)?"auth":"gauth";const t={cred:r};null===this.authOverride_?t.noauth=!0:"object"==typeof this.authOverride_&&(t.authvar=this.authOverride_),this.sendRequest(e,t,e=>{var t=e.s,n=e.d||"error";this.authToken_===r&&("ok"===t?this.invalidAuthTokenCount_=0:this.onAuthRevoked_(t,n))})}}tryAppCheck(){this.connected_&&this.appCheckToken_&&this.sendRequest("appcheck",{token:this.appCheckToken_},e=>{var t=e.s,n=e.d||"error";"ok"===t?this.invalidAppCheckTokenCount_=0:this.onAppCheckRevoked_(t,n)})}unlisten(e,t){var n=e._path.toString(),r=e._queryIdentifier;this.log_("Unlisten called for "+n+" "+r),p(e._queryParams.isDefault()||!e._queryParams.loadsAllData(),"unlisten() called for non-default but complete query"),this.removeListen_(n,r)&&this.connected_&&this.sendUnlisten_(n,r,e._queryObject,t)}sendUnlisten_(e,t,n,r){this.log_("Unlisten on "+e+" for "+t);const i={p:e};r&&(i.q=n,i.t=r),this.sendRequest("n",i)}onDisconnectPut(e,t,n){this.initConnection_(),this.connected_?this.sendOnDisconnect_("o",e,t,n):this.onDisconnectRequestQueue_.push({pathString:e,action:"o",data:t,onComplete:n})}onDisconnectMerge(e,t,n){this.initConnection_(),this.connected_?this.sendOnDisconnect_("om",e,t,n):this.onDisconnectRequestQueue_.push({pathString:e,action:"om",data:t,onComplete:n})}onDisconnectCancel(e,t){this.initConnection_(),this.connected_?this.sendOnDisconnect_("oc",e,null,t):this.onDisconnectRequestQueue_.push({pathString:e,action:"oc",data:null,onComplete:t})}sendOnDisconnect_(e,t,n,r){var i={p:t,d:n};this.log_("onDisconnect "+e,i),this.sendRequest(e,i,e=>{r&&setTimeout(()=>{r(e.s,e.d)},Math.floor(0))})}put(e,t,n,r){this.putInternal("p",e,t,n,r)}merge(e,t,n,r){this.putInternal("m",e,t,n,r)}putInternal(e,t,n,r,i){this.initConnection_();const s={p:t,d:n};void 0!==i&&(s.h=i),this.outstandingPuts_.push({action:e,request:s,onComplete:r}),this.outstandingPutCount_++;var o=this.outstandingPuts_.length-1;this.connected_?this.sendPut_(o):this.log_("Buffering put: "+t)}sendPut_(t){const n=this.outstandingPuts_[t].action;var e=this.outstandingPuts_[t].request;const r=this.outstandingPuts_[t].onComplete;this.outstandingPuts_[t].queued=this.connected_,this.sendRequest(n,e,e=>{this.log_(n+" response",e),delete this.outstandingPuts_[t],this.outstandingPutCount_--,0===this.outstandingPutCount_&&(this.outstandingPuts_=[]),r&&r(e.s,e.d)})}reportStats(e){var t;this.connected_&&(this.log_("reportStats",t={c:e}),this.sendRequest("s",t,e=>{var t;"ok"!==e.s&&(t=e.d,this.log_("reportStats","Error sending stats: "+t))}))}onDataMessage_(e){if("r"in e){this.log_("from server: "+v(e));var t=e.r;const n=this.requestCBHash_[t];n&&(delete this.requestCBHash_[t],n(e.b))}else{if("error"in e)throw"A server-side error has occurred: "+e.error;"a"in e&&this.onDataPush_(e.a,e.b)}}onDataPush_(e,t){this.log_("handleServerMessage",e,t),"d"===e?this.onDataUpdate_(t.p,t.d,!1,t.t):"m"===e?this.onDataUpdate_(t.p,t.d,!0,t.t):"c"===e?this.onListenRevoked_(t.p,t.q):"ac"===e?this.onAuthRevoked_(t.s,t.d):"apc"===e?this.onAppCheckRevoked_(t.s,t.d):"sd"===e?this.onSecurityDebugPacket_(t):le("Unrecognized action received from server: "+v(e)+"\nAre you using the latest client?")}onReady_(e,t){this.log_("connection ready"),this.connected_=!0,this.lastConnectionEstablishedTime_=(new Date).getTime(),this.handleTimestamp_(e),this.lastSessionId=t,this.firstConnection_&&this.sendConnectStats_(),this.restoreState_(),this.firstConnection_=!1,this.onConnectStatus_(!0)}scheduleConnect_(e){p(!this.realtime_,"Scheduling a connect when we're already connected/ing?"),this.establishConnectionTimer_&&clearTimeout(this.establishConnectionTimer_),this.establishConnectionTimer_=setTimeout(()=>{this.establishConnectionTimer_=null,this.establishConnection_()},Math.floor(e))}initConnection_(){!this.realtime_&&this.firstConnection_&&this.scheduleConnect_(0)}onVisible_(e){e&&!this.visible_&&this.reconnectDelay_===this.maxReconnectDelay_&&(this.log_("Window became visible. Reducing delay."),this.reconnectDelay_=1e3,this.realtime_||this.scheduleConnect_(0)),this.visible_=e}onOnline_(e){e?(this.log_("Browser went online."),this.reconnectDelay_=1e3,this.realtime_||this.scheduleConnect_(0)):(this.log_("Browser went offline. Killing connection."),this.realtime_&&this.realtime_.close())}onRealtimeDisconnect_(){var e;this.log_("data client disconnected"),this.connected_=!1,this.realtime_=null,this.cancelSentTransactions_(),this.requestCBHash_={},this.shouldReconnect_()&&(this.visible_?this.lastConnectionEstablishedTime_&&(3e4<(new Date).getTime()-this.lastConnectionEstablishedTime_&&(this.reconnectDelay_=1e3),this.lastConnectionEstablishedTime_=null):(this.log_("Window isn't visible. Delaying reconnect."),this.reconnectDelay_=this.maxReconnectDelay_,this.lastConnectionAttemptTime_=(new Date).getTime()),e=(new Date).getTime()-this.lastConnectionAttemptTime_,e=Math.max(0,this.reconnectDelay_-e),e=Math.random()*e,this.log_("Trying to reconnect in "+e+"ms"),this.scheduleConnect_(e),this.reconnectDelay_=Math.min(this.maxReconnectDelay_,1.3*this.reconnectDelay_)),this.onConnectStatus_(!1)}async establishConnection_(){if(this.shouldReconnect_()){this.log_("Making a connection attempt"),this.lastConnectionAttemptTime_=(new Date).getTime(),this.lastConnectionEstablishedTime_=null;var e=this.onDataMessage_.bind(this),r=this.onReady_.bind(this);const c=this.onRealtimeDisconnect_.bind(this);var i=this.id+":"+gt.nextConnectionId_++,s=this.lastSessionId;let t=!1,n=null;var o=function(){n?n.close():(t=!0,c())};this.realtime_={close:o,sendRequest:function(e){p(n,"sendRequest call when we're not connected not allowed."),n.sendRequest(e)}};var a=this.forceTokenRefresh_;this.forceTokenRefresh_=!1;try{var[l,h]=await Promise.all([this.authTokenProvider_.getToken(a),this.appCheckTokenProvider_.getToken(a)]);t?pe("getToken() completed but was canceled"):(pe("getToken() completed. Creating connection."),this.authToken_=l&&l.accessToken,this.appCheckToken_=h&&h.token,n=new Ke(i,this.repoInfo_,this.applicationId_,this.appCheckToken_,this.authToken_,e,r,c,e=>{ge(e+" ("+this.repoInfo_.toString()+")"),this.interrupt("server_kill")},s))}catch(e){this.log_("Failed to get token: "+e),t||(this.repoInfo_.nodeAdmin&&ge(e),o())}}}interrupt(e){pe("Interrupting connection for reason: "+e),this.interruptReasons_[e]=!0,this.realtime_?this.realtime_.close():(this.establishConnectionTimer_&&(clearTimeout(this.establishConnectionTimer_),this.establishConnectionTimer_=null),this.connected_&&this.onRealtimeDisconnect_())}resume(e){pe("Resuming connection for reason: "+e),delete this.interruptReasons_[e],b(this.interruptReasons_)&&(this.reconnectDelay_=1e3,this.realtime_||this.scheduleConnect_(0))}handleTimestamp_(e){var t=e-(new Date).getTime();this.onServerInfoUpdate_({serverTimeOffset:t})}cancelSentTransactions_(){for(let e=0;eue(e)).join("$"):"default";const r=this.removeListen_(e,n);r&&r.onComplete&&r.onComplete("permission_denied")}removeListen_(e,t){var n=new Xe(e).toString();let r;if(this.listens.has(n)){const i=this.listens.get(n);r=i.get(t),i.delete(t),0===i.size&&this.listens.delete(n)}else r=void 0;return r}onAuthRevoked_(e,t){pe("Auth token revoked: "+e+"/"+t),this.authToken_=null,this.forceTokenRefresh_=!0,this.realtime_.close(),"invalid_token"!==e&&"permission_denied"!==e||(this.invalidAuthTokenCount_++,3<=this.invalidAuthTokenCount_&&(this.reconnectDelay_=3e4,this.authTokenProvider_.notifyForInvalidToken()))}onAppCheckRevoked_(e,t){pe("App check token revoked: "+e+"/"+t),this.appCheckToken_=null,this.forceTokenRefresh_=!0,"invalid_token"!==e&&"permission_denied"!==e||(this.invalidAppCheckTokenCount_++,3<=this.invalidAppCheckTokenCount_&&this.appCheckTokenProvider_.notifyForInvalidToken())}onSecurityDebugPacket_(e){this.securityDebugCallback_?this.securityDebugCallback_(e):"msg"in e&&console.log("FIREBASE: "+e.msg.replace("\n","\nFIREBASE: "))}restoreState_(){this.tryAuth(),this.tryAppCheck();for(const t of this.listens.values())for(const n of t.values())this.sendListen_(n);for(let r=0;r{const n=C(this.indexSet_,e);if(p(n,"Missing index implementation for "+e),t===Wt){if(n.isDefinedOn(o.node)){const i=[],s=a.getIterator(mt.Wrap);let e=s.getNext();for(;e;)e.name!==o.name&&i.push(e),e=s.getNext();return i.push(o),Ft(i,n.getCompare())}return Wt}{var r=a.get(o.name);let e=t;return r&&(e=e.remove(new mt(o.name,r))),e.insert(o,o.node)}});return new Ut(e,this.indexSet_)}removeFromIndexes(n,r){var e=I(this.indexes_,e=>{if(e===Wt)return e;var t=r.get(n.name);return t?e.remove(new mt(n.name,t)):e});return new Ut(e,this.indexSet_)}}let Bt;class jt{constructor(e,t,n){this.children_=e,this.priorityNode_=t,this.indexMap_=n,this.lazyHash_=null,this.priorityNode_&&Pt(this.priorityNode_),this.children_.isEmpty()&&p(!this.priorityNode_||this.priorityNode_.isEmpty(),"An empty node cannot have a priority")}static get EMPTY_NODE(){return Bt=Bt||new jt(new Tt(St),null,Ut.Default)}isLeafNode(){return!1}getPriority(){return this.priorityNode_||Bt}updatePriority(e){return this.children_.isEmpty()?this:new jt(this.children_,e,this.indexMap_)}getImmediateChild(e){if(".priority"===e)return this.getPriority();var t=this.children_.get(e);return null===t?Bt:t}getChild(e){var t=et(e);return null===t?this:this.getImmediateChild(t).getChild(nt(e))}hasChild(e){return null!==this.children_.get(e)}updateImmediateChild(n,r){if(p(r,"We should always be passing snapshot nodes"),".priority"===n)return this.updatePriority(r);{var i=new mt(n,r);let e,t;t=r.isEmpty()?(e=this.children_.remove(n),this.indexMap_.removeFromIndexes(i,this.children_)):(e=this.children_.insert(n,r),this.indexMap_.addToIndexes(i,this.children_));i=e.isEmpty()?Bt:this.priorityNode_;return new jt(e,i,t)}}updateChild(e,t){var n=et(e);if(null===n)return t;p(".priority"!==et(e)||1===tt(e),".priority must be the last token in a path");var r=this.getImmediateChild(n).updateChild(nt(e),t);return this.updateImmediateChild(n,r)}isEmpty(){return this.children_.isEmpty()}numChildren(){return this.children_.count()}val(n){if(this.isEmpty())return null;const r={};let i=0,s=0,o=!0;if(this.forEachChild(Ot,(e,t)=>{r[e]=t.val(n),i++,o&&jt.INTEGER_REGEXP_.test(e)?s=Math.max(s,Number(e)):o=!1}),!n&&o&&s<2*i){const e=[];for(const t in r)e[t]=r[t];return e}return n&&!this.getPriority().isEmpty()&&(r[".priority"]=this.getPriority().val()),r}hash(){if(null===this.lazyHash_){let r="";this.getPriority().isEmpty()||(r+="priority:"+Nt(this.getPriority().val())+":"),this.forEachChild(Ot,(e,t)=>{var n=t.hash();""!==n&&(r+=":"+e+":"+n)}),this.lazyHash_=""===r?"":Z(r)}return this.lazyHash_}getPredecessorChildName(e,t,n){const r=this.resolveIndex_(n);if(r){var i=r.getPredecessorKey(new mt(e,t));return i?i.name:null}return this.children_.getPredecessorKey(e)}getFirstChildName(e){const t=this.resolveIndex_(e);if(t){var n=t.minKey();return n&&n.name}return this.children_.minKey()}getFirstChild(e){var t=this.getFirstChildName(e);return t?new mt(t,this.children_.get(t)):null}getLastChildName(e){const t=this.resolveIndex_(e);if(t){var n=t.maxKey();return n&&n.name}return this.children_.maxKey()}getLastChild(e){var t=this.getLastChildName(e);return t?new mt(t,this.children_.get(t)):null}forEachChild(e,t){const n=this.resolveIndex_(e);return n?n.inorderTraversal(e=>t(e.name,e.node)):this.children_.inorderTraversal(t)}getIterator(e){return this.getIteratorFrom(e.minPost(),e)}getIteratorFrom(t,n){const e=this.resolveIndex_(n);if(e)return e.getIteratorFrom(t,e=>e);{const r=this.children_.getIteratorFrom(t.name,mt.Wrap);let e=r.peek();for(;null!=e&&n.compare(e,t)<0;)r.getNext(),e=r.peek();return r}}getReverseIterator(e){return this.getReverseIteratorFrom(e.maxPost(),e)}getReverseIteratorFrom(t,n){const e=this.resolveIndex_(n);if(e)return e.getReverseIteratorFrom(t,e=>e);{const r=this.children_.getReverseIteratorFrom(t.name,mt.Wrap);let e=r.peek();for(;null!=e&&0{if(w(i,e)&&"."!==e.substring(0,1)){const n=Qt(t);!n.isLeafNode()&&n.isEmpty()||(r=r.updateImmediateChild(e,n))}}),r.updatePriority(Qt(e))}{const s=[];let r=!1;if(be(i,(e,t)=>{if("."!==e.substring(0,1)){const n=Qt(t);n.isEmpty()||(r=r||!n.getPriority().isEmpty(),s.push(new mt(e,n)))}}),0===s.length)return jt.EMPTY_NODE;var n=Ft(s,Et,e=>e.name,St);if(r){t=Ft(s,Ot.getCompare());return new jt(n,Qt(e),new Ut({".priority":t},{".priority":Ot}))}return new jt(n,Qt(e),Ut.Default)}}Dt=Qt;class Yt extends vt{constructor(e){super(),this.indexPath_=e,p(!at(e)&&".priority"!==et(e),"Can't create PathIndex with empty path or .priority key")}extractChild(e){return e.getChild(this.indexPath_)}isDefinedOn(e){return!e.getChild(this.indexPath_).isEmpty()}compare(e,t){const n=this.extractChild(e.node);var r=this.extractChild(t.node),r=n.compareTo(r);return 0===r?Ce(e.name,t.name):r}makePost(e,t){var n=Qt(e),n=jt.EMPTY_NODE.updateChild(this.indexPath_,n);return new mt(t,n)}maxPost(){var e=jt.EMPTY_NODE.updateChild(this.indexPath_,zt);return new mt(we,e)}toString(){return it(this.indexPath_,0).join("/")}}const Kt=new class extends vt{compare(e,t){var n=e.node.compareTo(t.node);return 0===n?Ce(e.name,t.name):n}isDefinedOn(e){return!0}indexedValueChanged(e,t){return!e.equals(t)}minPost(){return mt.MIN}maxPost(){return mt.MAX}makePost(e,t){var n=Qt(e);return new mt(t,n)}toString(){return".value"}};function $t(e){return{type:"value",snapshotNode:e}}function Gt(e,t){return{type:"child_added",snapshotNode:t,childName:e}}function Jt(e,t){return{type:"child_removed",snapshotNode:t,childName:e}}function Xt(e,t,n){return{type:"child_changed",snapshotNode:t,childName:e,oldSnap:n}}class Zt{constructor(e){this.index_=e}updateChild(e,t,n,r,i,s){p(e.isIndexed(this.index_),"A node must be indexed if only a child is updated");const o=e.getImmediateChild(t);return o.getChild(r).equals(n.getChild(r))&&o.isEmpty()===n.isEmpty()?e:(null!=s&&(n.isEmpty()?e.hasChild(t)?s.trackChildChange(Jt(t,o)):p(e.isLeafNode(),"A child remove without an old child only makes sense on a leaf node"):o.isEmpty()?s.trackChildChange(Gt(t,n)):s.trackChildChange(Xt(t,n,o))),e.isLeafNode()&&n.isEmpty()?e:e.updateImmediateChild(t,n).withIndex(this.index_))}updateFullNode(r,n,i){return null!=i&&(r.isLeafNode()||r.forEachChild(Ot,(e,t)=>{n.hasChild(e)||i.trackChildChange(Jt(e,t))}),n.isLeafNode()||n.forEachChild(Ot,(e,t)=>{if(r.hasChild(e)){const n=r.getImmediateChild(e);n.equals(t)||i.trackChildChange(Xt(e,t,n))}else i.trackChildChange(Gt(e,t))})),n.withIndex(this.index_)}updatePriority(e,t){return e.isEmpty()?jt.EMPTY_NODE:e.updatePriority(t)}filtersNodes(){return!1}getIndexedFilter(){return this}getIndex(){return this.index_}}class en{constructor(e){this.indexedFilter_=new Zt(e.getIndex()),this.index_=e.getIndex(),this.startPost_=en.getStartPost_(e),this.endPost_=en.getEndPost_(e),this.startIsInclusive_=!e.startAfterSet_,this.endIsInclusive_=!e.endBeforeSet_}getStartPost(){return this.startPost_}getEndPost(){return this.endPost_}matches(e){var t=this.startIsInclusive_?this.index_.compare(this.getStartPost(),e)<=0:this.index_.compare(this.getStartPost(),e)<0,n=this.endIsInclusive_?this.index_.compare(e,this.getEndPost())<=0:this.index_.compare(e,this.getEndPost())<0;return t&&n}updateChild(e,t,n,r,i,s){return this.matches(new mt(t,n))||(n=jt.EMPTY_NODE),this.indexedFilter_.updateChild(e,t,n,r,i,s)}updateFullNode(e,t,n){let r=(t=t.isLeafNode()?jt.EMPTY_NODE:t).withIndex(this.index_);r=r.updatePriority(jt.EMPTY_NODE);const i=this;return t.forEachChild(Ot,(e,t)=>{i.matches(new mt(e,t))||(r=r.updateImmediateChild(e,jt.EMPTY_NODE))}),this.indexedFilter_.updateFullNode(e,r,n)}updatePriority(e,t){return e}filtersNodes(){return!0}getIndexedFilter(){return this.indexedFilter_}getIndex(){return this.index_}static getStartPost_(e){if(e.hasStart()){var t=e.getIndexStartName();return e.getIndex().makePost(e.getIndexStartValue(),t)}return e.getIndex().minPost()}static getEndPost_(e){if(e.hasEnd()){var t=e.getIndexEndName();return e.getIndex().makePost(e.getIndexEndValue(),t)}return e.getIndex().maxPost()}}class tn{constructor(e){this.withinDirectionalStart=e=>this.reverse_?this.withinEndPost(e):this.withinStartPost(e),this.withinDirectionalEnd=e=>this.reverse_?this.withinStartPost(e):this.withinEndPost(e),this.withinStartPost=e=>{var t=this.index_.compare(this.rangedFilter_.getStartPost(),e);return this.startIsInclusive_?t<=0:t<0},this.withinEndPost=e=>{var t=this.index_.compare(e,this.rangedFilter_.getEndPost());return this.endIsInclusive_?t<=0:t<0},this.rangedFilter_=new en(e),this.index_=e.getIndex(),this.limit_=e.getLimit(),this.reverse_=!e.isViewFromLeft(),this.startIsInclusive_=!e.startAfterSet_,this.endIsInclusive_=!e.endBeforeSet_}updateChild(e,t,n,r,i,s){return this.rangedFilter_.matches(new mt(t,n))||(n=jt.EMPTY_NODE),e.getImmediateChild(t).equals(n)?e:e.numChildren()d(t,e)}else s=this.index_.getCompare();const o=e;p(o.numChildren()===this.limit_,"");var a=new mt(t,n),l=this.reverse_?o.getFirstChild(this.index_):o.getLastChild(this.index_),h=this.rangedFilter_.matches(a);if(o.hasChild(t)){var c=o.getImmediateChild(t);let e=r.getChildAfterChild(this.index_,l,this.reverse_);for(;null!=e&&(e.name===t||o.hasChild(e.name));)e=r.getChildAfterChild(this.index_,e,this.reverse_);var u=null==e?1:s(e,a);if(h&&!n.isEmpty()&&0<=u)return null!=i&&i.trackChildChange(Xt(t,n,c)),o.updateImmediateChild(t,n);{null!=i&&i.trackChildChange(Jt(t,c));const _=o.updateImmediateChild(t,jt.EMPTY_NODE);return null!=e&&this.rangedFilter_.matches(e)?(null!=i&&i.trackChildChange(Gt(e.name,e.node)),_.updateImmediateChild(e.name,e.node)):_}}return!n.isEmpty()&&h&&0<=s(l,a)?(null!=i&&(i.trackChildChange(Jt(l.name,l.node)),i.trackChildChange(Gt(t,n))),o.updateImmediateChild(t,n).updateImmediateChild(l.name,jt.EMPTY_NODE)):e}}class nn{constructor(){this.limitSet_=!1,this.startSet_=!1,this.startNameSet_=!1,this.startAfterSet_=!1,this.endSet_=!1,this.endNameSet_=!1,this.endBeforeSet_=!1,this.limit_=0,this.viewFrom_="",this.indexStartValue_=null,this.indexStartName_="",this.indexEndValue_=null,this.indexEndName_="",this.index_=Ot}hasStart(){return this.startSet_}isViewFromLeft(){return""===this.viewFrom_?this.startSet_:"l"===this.viewFrom_}getIndexStartValue(){return p(this.startSet_,"Only valid if start has been set"),this.indexStartValue_}getIndexStartName(){return p(this.startSet_,"Only valid if start has been set"),this.startNameSet_?this.indexStartName_:ye}hasEnd(){return this.endSet_}getIndexEndValue(){return p(this.endSet_,"Only valid if end has been set"),this.indexEndValue_}getIndexEndName(){return p(this.endSet_,"Only valid if end has been set"),this.endNameSet_?this.indexEndName_:we}hasLimit(){return this.limitSet_}hasAnchoredLimit(){return this.limitSet_&&""!==this.viewFrom_}getLimit(){return p(this.limitSet_,"Only valid if limit has been set"),this.limit_}getIndex(){return this.index_}loadsAllData(){return!(this.startSet_||this.endSet_||this.limitSet_)}isDefault(){return this.loadsAllData()&&this.index_===Ot}copy(){const e=new nn;return e.limitSet_=this.limitSet_,e.limit_=this.limit_,e.startSet_=this.startSet_,e.startAfterSet_=this.startAfterSet_,e.indexStartValue_=this.indexStartValue_,e.startNameSet_=this.startNameSet_,e.indexStartName_=this.indexStartName_,e.endSet_=this.endSet_,e.endBeforeSet_=this.endBeforeSet_,e.indexEndValue_=this.indexEndValue_,e.endNameSet_=this.endNameSet_,e.indexEndName_=this.indexEndName_,e.index_=this.index_,e.viewFrom_=this.viewFrom_,e}}function rn(e,t,n){const r=e.copy();return r.startSet_=!0,void 0===t&&(t=null),r.indexStartValue_=t,null!=n?(r.startNameSet_=!0,r.indexStartName_=n):(r.startNameSet_=!1,r.indexStartName_=""),r}function sn(e,t,n){const r=e.copy();return r.endSet_=!0,void 0===t&&(t=null),r.indexEndValue_=t,void 0!==n?(r.endNameSet_=!0,r.indexEndName_=n):(r.endNameSet_=!1,r.indexEndName_=""),r}function on(e,t){const n=e.copy();return n.index_=t,n}function an(e){const t={};if(e.isDefault())return t;let n;var r;return n=e.index_===Ot?"$priority":e.index_===Kt?"$value":e.index_===Ct?"$key":(p(e.index_ instanceof Yt,"Unrecognized index type!"),e.index_.toString()),t.orderBy=v(n),e.startSet_&&(r=e.startAfterSet_?"startAfter":"startAt",t[r]=v(e.indexStartValue_),e.startNameSet_&&(t[r]+=","+v(e.indexStartName_))),e.endSet_&&(r=e.endBeforeSet_?"endBefore":"endAt",t[r]=v(e.indexEndValue_),e.endNameSet_&&(t[r]+=","+v(e.indexEndName_))),e.limitSet_&&(e.isViewFromLeft()?t.limitToFirst=e.limit_:t.limitToLast=e.limit_),t}function ln(t){const n={};if(t.startSet_&&(n.sp=t.indexStartValue_,t.startNameSet_&&(n.sn=t.indexStartName_),n.sin=!t.startAfterSet_),t.endSet_&&(n.ep=t.indexEndValue_,t.endNameSet_&&(n.en=t.indexEndName_),n.ein=!t.endBeforeSet_),t.limitSet_){n.l=t.limit_;let e=t.viewFrom_;""===e&&(e=t.isViewFromLeft()?"l":"r"),n.vf=e}return t.index_!==Ot&&(n.i=t.index_.toString()),n}class hn extends $e{constructor(e,t,n,r){super(),this.repoInfo_=e,this.onDataUpdate_=t,this.authTokenProvider_=n,this.appCheckTokenProvider_=r,this.log_=ae("p:rest:"),this.listens_={}}reportStats(e){throw new Error("Method not implemented.")}static getListenId_(e,t){return void 0!==t?"tag$"+t:(p(e._queryParams.isDefault(),"should have a tag if it's not a default query."),e._path.toString())}listen(e,t,r,i){const s=e._path.toString();this.log_("Listen called for "+s+" "+e._queryIdentifier);const o=hn.getListenId_(e,r),a={};this.listens_[o]=a;var n=an(e._queryParams);this.restRequest_(s+".json",n,(t,e)=>{let n=e;if(null===(t=404===t?n=null:t)&&this.onDataUpdate_(s,n,!1,r),C(this.listens_,o)===a){let e;e=t?401===t?"permission_denied":"rest_error:"+t:"ok",i(e,null)}})}unlisten(e,t){var n=hn.getListenId_(e,t);delete this.listens_[n]}get(e){var t=an(e._queryParams);const r=e._path.toString(),i=new _;return this.restRequest_(r+".json",t,(e,t)=>{let n=t;null===(e=404===e?n=null:e)?(this.onDataUpdate_(r,n,!1,null),i.resolve(n)):i.reject(new Error(n))}),i.promise}refreshAuthToken(e){}restRequest_(i,s={},o){return s.format="export",Promise.all([this.authTokenProvider_.getToken(!1),this.appCheckTokenProvider_.getToken(!1)]).then(([e,t])=>{e&&e.accessToken&&(s.auth=e.accessToken),t&&t.token&&(s.ac=t.token);const n=(this.repoInfo_.secure?"https://":"http://")+this.repoInfo_.host+i+"?ns="+this.repoInfo_.namespace+function(e){const t=[];for(const[n,r]of Object.entries(e))Array.isArray(r)?r.forEach(e=>{t.push(encodeURIComponent(n)+"="+encodeURIComponent(e))}):t.push(encodeURIComponent(n)+"="+encodeURIComponent(r));return t.length?"&"+t.join("&"):""}(s);this.log_("Sending REST request for "+n);const r=new XMLHttpRequest;r.onreadystatechange=()=>{if(o&&4===r.readyState){this.log_("REST Response for "+n+" received. status:",r.status,"response:",r.responseText);let e=null;if(200<=r.status&&r.status<300){try{e=m(r.responseText)}catch(e){ge("Failed to parse JSON response for "+n+": "+r.responseText)}o(null,e)}else 401!==r.status&&404!==r.status&&ge("Got unsuccessful REST response for "+n+" Status: "+r.status),o(r.status);o=null}},r.open("GET",n,!0),r.send()})}}class cn{constructor(){this.rootNode_=jt.EMPTY_NODE}getNode(e){return this.rootNode_.getChild(e)}updateSnapshot(e,t){this.rootNode_=this.rootNode_.updateChild(e,t)}}function un(){return{value:null,children:new Map}}function dn(e,t,n){var r;at(t)?(e.value=n,e.children.clear()):null!==e.value?e.value=e.value.updateChild(t,n):(r=et(t),e.children.has(r)||e.children.set(r,un()),dn(e.children.get(r),t=nt(t),n))}function _n(e,n,r){var i;null!==e.value?r(n,e.value):(i=(e,t)=>{_n(t,new Xe(n.toString()+"/"+e),r)},e.children.forEach((e,t)=>{i(t,e)}))}class pn{constructor(e){this.collection_=e,this.last_=null}get(){var e=this.collection_.get();const n=Object.assign({},e);return this.last_&&be(this.last_,(e,t)=>{n[e]=n[e]-t}),this.last_=e,n}}class fn{constructor(e,t){this.server_=t,this.statsToReport_={},this.statsListener_=new pn(e);var n=1e4+2e4*Math.random();Ee(this.reportStats_.bind(this),Math.floor(n))}reportStats_(){var e=this.statsListener_.get();const n={};let r=!1;be(e,(e,t)=>{0{var t;"child_changed"===e.type&&n.index_.indexedValueChanged(e.oldSnap,e.snapshotNode)&&s.push((t=e.childName,{type:"child_moved",snapshotNode:e.snapshotNode,childName:t}))}),Sn(n,i,"child_removed",e,r,t),Sn(n,i,"child_added",e,r,t),Sn(n,i,"child_moved",s,r,t),Sn(n,i,"child_changed",e,r,t),Sn(n,i,"value",e,r,t),i}function Sn(s,o,t,e,a,l){const n=e.filter(e=>e.type===t);n.sort((e,t)=>function(e,t,n){if(null==t.childName||null==n.childName)throw c("Should only compare child_ events.");var r=new mt(t.childName,t.snapshotNode),i=new mt(n.childName,n.snapshotNode);return e.index_.compare(r,i)}(s,e,t)),n.forEach(t=>{const n=(e=s,i=l,"value"===(r=t).type||"child_removed"===r.type||(r.prevName=i.getPredecessorChildName(r.childName,r.snapshotNode,e.index_)),r);var e,r,i;a.forEach(e=>{e.respondsTo(t.type)&&o.push(e.createEvent(n,s.query_))})})}function kn(e,t){return{eventCache:e,serverCache:t}}function Nn(e,t,n,r){return kn(new In(t,n,r),e.serverCache)}function Pn(e,t,n,r){return kn(e.eventCache,new In(t,n,r))}function Rn(e){return e.eventCache.isFullyInitialized()?e.eventCache.getNode():null}function xn(e){return e.serverCache.isFullyInitialized()?e.serverCache.getNode():null}let Dn;class An{constructor(e,t=(Dn=Dn||new Tt(he),Dn)){this.value=e,this.children=t}static fromObject(e){let n=new An(null);return be(e,(e,t)=>{n=n.set(new Xe(e),t)}),n}isEmpty(){return null===this.value&&this.children.isEmpty()}findRootMostMatchingPathAndValue(e,t){if(null!=this.value&&t(this.value))return{path:Ze(),value:this.value};if(at(e))return null;{var n=et(e);const i=this.children.get(n);if(null===i)return null;var r=i.findRootMostMatchingPathAndValue(nt(e),t);return null==r?null:{path:ot(new Xe(n),r.path),value:r.value}}}findRootMostValueAndPath(e){return this.findRootMostMatchingPathAndValue(e,()=>!0)}subtree(e){if(at(e))return this;{var t=et(e);const n=this.children.get(t);return null!==n?n.subtree(nt(e)):new An(null)}}set(e,t){if(at(e))return new An(t,this.children);{var n=et(e);const i=this.children.get(n)||new An(null);var r=i.set(nt(e),t),r=this.children.insert(n,r);return new An(this.value,r)}}remove(t){if(at(t))return this.children.isEmpty()?new An(null):new An(null,this.children);{var n=et(t);const r=this.children.get(n);if(r){const i=r.remove(nt(t));let e;return e=i.isEmpty()?this.children.remove(n):this.children.insert(n,i),null===this.value&&e.isEmpty()?new An(null):new An(this.value,e)}return this}}get(e){if(at(e))return this.value;{var t=et(e);const n=this.children.get(t);return n?n.get(nt(e)):null}}setTree(t,n){if(at(t))return n;{var r=et(t);const i=this.children.get(r)||new An(null),s=i.setTree(nt(t),n);let e;return e=s.isEmpty()?this.children.remove(r):this.children.insert(r,s),new An(this.value,e)}}fold(e){return this.fold_(Ze(),e)}fold_(n,r){const i={};return this.children.inorderTraversal((e,t)=>{i[e]=t.fold_(ot(n,e),r)}),r(n,this.value,i)}findOnPath(e,t){return this.findOnPath_(e,Ze(),t)}findOnPath_(e,t,n){var r=!!this.value&&n(t,this.value);if(r)return r;if(at(e))return null;{r=et(e);const i=this.children.get(r);return i?i.findOnPath_(nt(e),ot(t,r),n):null}}foreachOnPath(e,t){return this.foreachOnPath_(e,Ze(),t)}foreachOnPath_(e,t,n){if(at(e))return this;{this.value&&n(t,this.value);var r=et(e);const i=this.children.get(r);return i?i.foreachOnPath_(nt(e),ot(t,r),n):new An(null)}}foreach(e){this.foreach_(Ze(),e)}foreach_(n,r){this.children.inorderTraversal((e,t)=>{t.foreach_(ot(n,e),r)}),this.value&&r(n,this.value)}foreachChild(n){this.children.inorderTraversal((e,t)=>{t.value&&n(e,t.value)})}}class On{constructor(e){this.writeTree_=e}static empty(){return new On(new An(null))}}function Ln(t,n,r){if(at(n))return new On(new An(r));var i=t.writeTree_.findRootMostValueAndPath(n);if(null!=i){var s=i.path;let e=i.value;i=lt(s,n);return e=e.updateChild(i,r),new On(t.writeTree_.set(s,e))}s=new An(r),s=t.writeTree_.setTree(n,s);return new On(s)}function Mn(e,n,t){let r=e;return be(t,(e,t)=>{r=Ln(r,ot(n,e),t)}),r}function Fn(e,t){if(at(t))return On.empty();var n=e.writeTree_.setTree(t,new An(null));return new On(n)}function qn(e,t){return null!=Wn(e,t)}function Wn(e,t){var n=e.writeTree_.findRootMostValueAndPath(t);return null!=n?e.writeTree_.get(n.path).getChild(lt(n.path,t)):null}function Un(e){const n=[],t=e.writeTree_.value;return null!=t?t.isLeafNode()||t.forEachChild(Ot,(e,t)=>{n.push(new mt(e,t))}):e.writeTree_.children.inorderTraversal((e,t)=>{null!=t.value&&n.push(new mt(e,t.value))}),n}function Bn(e,t){if(at(t))return e;var n=Wn(e,t);return null!=n?new On(new An(n)):new On(e.writeTree_.subtree(t))}function jn(e){return e.writeTree_.isEmpty()}function Vn(e,t){return function r(i,e,s){{if(null!=e.value)return s.updateChild(i,e.value);{let n=null;return e.children.inorderTraversal((e,t)=>{".priority"===e?(p(null!==t.value,"Priority writes must always be leaf nodes"),n=t.value):s=r(ot(i,e),t,s)}),s=!s.getChild(i).isEmpty()&&null!==n?s.updateChild(ot(i,".priority"),n):s}}}(Ze(),e.writeTree_,t)}function zn(e,t){return nr(t,e)}function Hn(t,n){var e,r=t.allWrites.findIndex(e=>e.writeId===n);p(0<=r,"removeWrite called with nonexistent writeId.");const i=t.allWrites[r];t.allWrites.splice(r,1);let s=i.visible,o=!1,a=t.allWrites.length-1;for(;s&&0<=a;){var l=t.allWrites[a];l.visible&&(a>=r&&function(e,t){{if(e.snap)return ut(e.path,t);for(const n in e.children)if(e.children.hasOwnProperty(n)&&ut(ot(e.path,n),t))return!0;return!1}}(l,i.path)?s=!1:ut(i.path,l.path)&&(o=!0)),a--}return!!s&&(o?((e=t).visibleWrites=Yn(e.allWrites,Qn,Ze()),0{t.visibleWrites=Fn(t.visibleWrites,ot(i.path,e))}),!0)}function Qn(e){return e.visible}function Yn(e,t,n){let r=On.empty();for(let s=0;s{r=r.updateImmediateChild(e,t)}),r;if(n){const s=Bn(e.visibleWrites,t);return n.forEachChild(Ot,(e,t)=>{var n=Vn(Bn(s,new Xe(e)),t);r=r.updateImmediateChild(e,n)}),Un(s).forEach(e=>{r=r.updateImmediateChild(e.name,e.node)}),r}return Un(Bn(e.visibleWrites,t)).forEach(e=>{r=r.updateImmediateChild(e.name,e.node)}),r}(e.writeTree,e.treePath,t)}function Jn(e,t,n,r){return function(e,t,n,r,i){p(r||i,"Either existingEventSnap or existingServerSnap must exist");var s=ot(t,n);return qn(e.visibleWrites,s)?null:jn(s=Bn(e.visibleWrites,s))?i.getChild(n):Vn(s,i.getChild(n))}(e.writeTree,e.treePath,t,n,r)}function Xn(e,t){return n=e.writeTree,t=ot(e.treePath,t),Wn(n.visibleWrites,t);var n}function Zn(e,t,n,r,i,s){return function(e,t,n,r,i,s,o){let a;var l=Bn(e.visibleWrites,t),h=Wn(l,Ze());if(null!=h)a=h;else{if(null==n)return[];a=Vn(l,n)}if(a=a.withIndex(o),a.isEmpty()||a.isLeafNode())return[];{const c=[],u=o.getCompare(),d=s?a.getReverseIteratorFrom(r,o):a.getIteratorFrom(r,o);let e=d.getNext();for(;e&&c.length{var n=ot(s,e);cr(i,et(n))&&(h=hr(r,h,n,t,o,a,l))}),e.foreach((e,t)=>{var n=ot(s,e);cr(i,et(n))||(h=hr(r,h,n,t,o,a,l))}),h}(e,t,l.path,l.children,r,i,s):(p(l.source.fromServer,"Unknown source."),a=l.source.tagged||t.serverCache.isFiltered(),dr(e,t,l.path,l.children,r,i,a,s))}else if(n.type===K.ACK_USER_WRITE){var h=n;o=h.revert?function(n,r,i,s,e,o){let a;{if(null!=Xn(s,i))return r;{var l=new sr(s,r,e);const c=r.eventCache.getNode();let t;if(at(i)||".priority"===et(i)){let e;e=r.serverCache.isFullyInitialized()?$n(s,xn(r)):(h=r.serverCache.getNode(),p(h instanceof jt,"serverChildren would be complete if leaf node"),Gn(s,h)),e=e,t=n.filter.updateFullNode(c,e,o)}else{var h=et(i);let e=er(s,h,r.serverCache);null==e&&r.serverCache.isCompleteForChild(h)&&(e=c.getImmediateChild(h)),t=null!=e?n.filter.updateChild(c,h,e,nt(i),l,o):r.eventCache.getNode().hasChild(h)?n.filter.updateChild(c,h,jt.EMPTY_NODE,nt(i),l,o):c,t.isEmpty()&&r.serverCache.isFullyInitialized()&&(a=$n(s,xn(r)),a.isLeafNode()&&(t=n.filter.updateFullNode(t,a,o)))}return a=r.serverCache.isFullyInitialized()||null!=Xn(s,Ze()),Nn(r,t,a,n.filter.filtersNodes())}}}(e,t,h.path,r,i,s):function(e,t,i,n,s,o,a){if(null!=Xn(s,i))return t;const l=t.serverCache.isFiltered(),h=t.serverCache;{if(null!=n.value){if(at(i)&&h.isFullyInitialized()||h.isCompleteForPath(i))return lr(e,t,i,h.getNode().getChild(i),s,o,l,a);if(at(i)){let n=new An(null);return h.getNode().forEachChild(Ct,(e,t)=>{n=n.set(new Xe(e),t)}),dr(e,t,i,n,s,o,l,a)}return t}{let r=new An(null);return n.foreach((e,t)=>{var n=ot(i,e);h.isCompleteForPath(n)&&(r=r.set(e,h.getNode().getChild(n)))}),dr(e,t,i,r,s,o,l,a)}}}(e,t,h.path,h.affectedTree,r,i,s)}else{if(n.type!==K.LISTEN_COMPLETE)throw c("Unknown operation type: "+n.type);o=function(e,t,n,r,i){const s=t.serverCache,o=Pn(t,s.getNode(),s.isFullyInitialized()||at(n),s.isFiltered());return ar(e,o,n,r,ir,i)}(e,t,n.path,r,s)}h=s.getChanges();return function(e,t,n){const r=t.eventCache;if(r.isFullyInitialized()){var i=r.getNode().isLeafNode()||r.getNode().isEmpty();const s=Rn(e);(0{n=n.updateChild(e,t)}),n}function dr(r,i,e,t,s,o,a,l){if(i.serverCache.getNode().isEmpty()&&!i.serverCache.isFullyInitialized())return i;let h=i,n;n=at(e)?t:new An(null).setTree(e,t);const c=i.serverCache.getNode();return n.children.inorderTraversal((e,t)=>{var n;c.hasChild(e)&&(n=ur(0,i.serverCache.getNode().getImmediateChild(e),t),h=lr(r,h,new Xe(e),n,s,o,a,l))}),n.children.inorderTraversal((e,t)=>{var n=!i.serverCache.isCompleteForChild(e)&&null===t.value;c.hasChild(e)||n||(n=ur(0,i.serverCache.getNode().getImmediateChild(e),t),h=lr(r,h,new Xe(e),n,s,o,a,l))}),h}class _r{constructor(e,t){this.query_=e,this.eventRegistrations_=[];const n=this.query_._queryParams,r=new Zt(n.getIndex()),i=(e=n).loadsAllData()?new Zt(e.getIndex()):new(e.hasLimit()?tn:en)(e);this.processor_={filter:i};const s=t.serverCache,o=t.eventCache;var a=r.updateFullNode(jt.EMPTY_NODE,s.getNode(),null),l=i.updateFullNode(jt.EMPTY_NODE,o.getNode(),null),a=new In(a,s.isFullyInitialized(),r.filtersNodes()),l=new In(l,o.isFullyInitialized(),i.filtersNodes());this.viewCache_=kn(l,a),this.eventGenerator_=new Tn(this.query_)}get query(){return this.query_}}function pr(e){return 0===e.eventRegistrations_.length}function fr(n,r,i){const s=[];if(i){p(null==r,"A cancel should cancel all event registrations.");const o=n.query._path;n.eventRegistrations_.forEach(e=>{var t=e.createCancelEvent(i,o);t&&s.push(t)})}if(r){let e=[];for(let t=0;t{r.push(Gt(e,t))})}return n.isFullyInitialized()&&r.push($t(n.getNode())),mr(e,r,n.getNode(),t)}(o,n)}function Ir(e,t,n,r){var i=t._queryIdentifier;const s=[];let o=[];var a=Nr(e);if("default"===i)for(var[l,h]of e.views.entries())o=o.concat(fr(h,n,r)),pr(h)&&(e.views.delete(l),h.query._queryParams.loadsAllData()||s.push(h.query));else{const c=e.views.get(i);c&&(o=o.concat(fr(c,n,r)),pr(c)&&(e.views.delete(i),c.query._queryParams.loadsAllData()||s.push(c.query)))}return a&&!Nr(e)&&s.push((p(vr,"Reference.ts has not been loaded"),new vr(t._repo,t._path))),{removed:s,events:o}}function Tr(e){const t=[];for(const n of e.views.values())n.query._queryParams.loadsAllData()||t.push(n);return t}function Er(e,t){let n=null;for(const r of e.views.values())n=n||function(e,t){const n=xn(e.viewCache_);return n&&(e.query._queryParams.loadsAllData()||!at(t)&&!n.getImmediateChild(et(t)).isEmpty())?n.getChild(t):null}(r,t);return n}function Sr(e,t){const n=t._queryParams;if(n.loadsAllData())return Pr(e);var r=t._queryIdentifier;return e.views.get(r)}function kr(e,t){return null!=Sr(e,t)}function Nr(e){return null!=Pr(e)}function Pr(e){for(const t of e.views.values())if(t.query._queryParams.loadsAllData())return t;return null}let Rr;let xr=1;class Dr{constructor(e){this.listenProvider_=e,this.syncPointTree_=new An(null),this.pendingWriteTree_={visibleWrites:On.empty(),allWrites:[],lastWriteId:-1},this.tagToQueryMap=new Map,this.queryToTagMap=new Map}}function Ar(e,t,n,r,i){var s,o,a,l;return s=e.pendingWriteTree_,o=t,a=n,l=r,r=i,p(l>s.lastWriteId,"Stacking an older write on top of newer ones"),s.allWrites.push({path:o,snap:a,writeId:l,visible:r=void 0===r?!0:r}),r&&(s.visibleWrites=Ln(s.visibleWrites,o,a)),s.lastWriteId=l,i?jr(e,new Cn(gn(),t,n)):[]}function Or(e,t,n,r){var i,s,o;i=e.pendingWriteTree_,s=t,o=n,r=r,p(r>i.lastWriteId,"Stacking an older merge on top of newer ones"),i.allWrites.push({path:s,children:o,writeId:r,visible:!0}),i.visibleWrites=Mn(i.visibleWrites,s,o),i.lastWriteId=r;var a=An.fromObject(n);return jr(e,new bn(gn(),t,a))}function Lr(e,t,n=!1){var r=function(e,t){for(let r=0;r{t=t.set(new Xe(e),!0)}),jr(e,new yn(r.path,t,n))}return[]}function Mr(e,t,n){return jr(e,new Cn(mn(),t,n))}function Fr(n,e,t,r,i=!1){var s=e._path,o=n.syncPointTree_.get(s);let a=[];if(o&&("default"===e._queryIdentifier||kr(o,e))){var l=Ir(o,e,t,r);0===o.views.size&&(n.syncPointTree_=n.syncPointTree_.remove(s));const d=l.removed;if(a=l.events,!i){o=-1!==d.findIndex(e=>e._queryParams.loadsAllData()),l=n.syncPointTree_.findOnPath(s,(e,t)=>Nr(t));if(o&&!l){const _=n.syncPointTree_.subtree(s);if(!_.isEmpty()){var h=_.fold((e,t,r)=>{if(t&&Nr(t))return[Pr(t)];{let n=[];return t&&(n=Tr(t)),be(r,(e,t)=>{n=n.concat(t)}),n}});for(let e=0;e{var t=n.queryToTagMap.get(Qr(e));n.listenProvider_.stopListening(Gr(e),t)}))}!function(e,t){for(let i=0;i{var n=lt(e,i);s=s||Er(t,n),o=o||Nr(t)});let a=e.syncPointTree_.get(i);a?(o=o||Nr(a),s=s||Er(a,Ze())):(a=new yr,e.syncPointTree_=e.syncPointTree_.set(i,a));let l;if(null!=s)l=!0;else{l=!1,s=jt.EMPTY_NODE;const _=e.syncPointTree_.subtree(i);_.foreachChild((e,t)=>{var n=Er(t,Ze());n&&(s=s.updateImmediateChild(e,n))})}var h,c=kr(a,t);c||t._queryParams.loadsAllData()||(u=Qr(t),p(!e.queryToTagMap.has(u),"View does not exist, but we have a tag"),h=xr++,e.queryToTagMap.set(u,h),e.tagToQueryMap.set(h,u));var u=zn(e.pendingWriteTree_,i);let d=br(a,t,n,u,s,l);return c||o||r||(c=Sr(a,t),d=d.concat(function(t,e,n){const r=e._path,i=Hr(t,e),s=zr(t,n),o=t.listenProvider_.startListening(Gr(e),i,s.hashFn,s.onComplete),a=t.syncPointTree_.subtree(r);if(i)p(!Nr(a.value),"If we're adding a query, it shouldn't be shadowed");else{var l=a.fold((e,t,r)=>{if(!at(e)&&t&&Nr(t))return[Pr(t).query];{let n=[];return t&&(n=n.concat(Tr(t).map(e=>e.query))),be(r,(e,t)=>{n=n.concat(t)}),n}});for(let e=0;e{var n=Er(t,lt(e,r));if(n)return n});return Kn(n,r,i,t,!0)}function Br(e,t){const r=t._path;let i=null;e.syncPointTree_.foreachOnPath(r,(e,t)=>{var n=lt(e,r);i=i||Er(t,n)});let n=e.syncPointTree_.get(r);n?i=i||Er(n,Ze()):(n=new yr,e.syncPointTree_=e.syncPointTree_.set(r,n));var s=null!=i;const o=s?new In(i,!0,!1):null;var a=zn(e.pendingWriteTree_,t._path);return Rn(Cr(n,t,a,s?o.getNode():jt.EMPTY_NODE,s).viewCache_)}function jr(e,t){return function t(n,r,i,s){{if(at(n.path))return Vr(n,r,i,s);{const o=r.get(Ze());null==i&&null!=o&&(i=Er(o,Ze()));let e=[];const a=et(n.path),l=n.operationForChild(a),h=r.children.get(a);if(h&&l){const c=i?i.getImmediateChild(a):null,u=tr(s,a);e=e.concat(t(l,h,c,u))}return o&&(e=e.concat(wr(o,n,s,i))),e}}}(t,e.syncPointTree_,null,zn(e.pendingWriteTree_,Ze()))}function Vr(s,e,o,a){var t=e.get(Ze());null==o&&null!=t&&(o=Er(t,Ze()));let l=[];return e.children.inorderTraversal((e,t)=>{var n=o?o.getImmediateChild(e):null,r=tr(a,e),i=s.operationForChild(e);i&&(l=l.concat(Vr(i,t,n,r)))}),t&&(l=l.concat(wr(t,s,a,o))),l}function zr(i,t){const s=t.query,o=Hr(i,s);return{hashFn:()=>{const e=t.viewCache_.serverCache.getNode()||jt.EMPTY_NODE;return e.hash()},onComplete:e=>{if("ok"===e)return o?function(e,t,n){if(s=Yr(e,n)){var r=Kr(s),i=r.path,s=r.queryId,r=lt(i,t);return $r(e,i,new wn(vn(s),r))}return[]}(i,s._path,o):(t=i,n=s._path,jr(t,new wn(mn(),n)));var t,n,r=function(e,t){let n="Unknown Error";"too_big"===e?n="The data requested exceeds the maximum size that can be accessed with a single request.":"permission_denied"===e?n="Client doesn't have permission to access the desired data.":"unavailable"===e&&(n="The service is unavailable");const r=new Error(e+" at "+t._path.toString()+": "+n);return r.code=e.toUpperCase(),r}(e,s);return Fr(i,s,null,r)}}}function Hr(e,t){var n=Qr(t);return e.queryToTagMap.get(n)}function Qr(e){return e._path.toString()+"$"+e._queryIdentifier}function Yr(e,t){return e.tagToQueryMap.get(t)}function Kr(e){var t=e.indexOf("$");return p(-1!==t&&t{var n=si(t,r.getImmediateChild(e),i);n!==t&&(s=s.updateImmediateChild(e,n))}),s}}class oi{constructor(e="",t=null,n={children:{},childCount:0}){this.name=e,this.parent=t,this.node=n}}function ai(e,t){let n=t instanceof Xe?t:new Xe(t),r=e,i=et(n);for(;null!==i;){var s=C(r.node.children,i)||{children:{},childCount:0};r=new oi(i,r,s),n=nt(n),i=et(n)}return r}function li(e){return e.node.value}function hi(e,t){e.node.value=t,_i(e)}function ci(e){return 0{r(new oi(e,n,t))})}function di(e){return new Xe(null===e.parent?e.name:di(e.parent)+"/"+e.name)}function _i(e){var t,n,r,i;null!==e.parent&&(t=e.parent,n=e.name,r=function(e){return void 0===li(e)&&!ci(e)}(e=e),i=w(t.node.children,n),r&&i?(delete t.node.children[n],t.node.childCount--,_i(t)):r||i||(t.node.children[n]=e.node,t.node.childCount++,_i(t)))}function pi(e,t,n,r){r&&void 0===t||ki(S(e,"value"),t,n)}function fi(e,t,r,n){if(!n||void 0!==t){const i=S(e,"values");if(!t||"object"!=typeof t||Array.isArray(t))throw new Error(i+" must be an object containing the children to replace.");const s=[];be(t,(e,t)=>{const n=new Xe(e);if(ki(i,t,ot(r,n)),".priority"===rt(n)&&!Si(t))throw new Error(i+"contains an invalid value for '"+n.toString()+"', which must be a valid Firebase priority (a string, finite number, server value, or null).");s.push(n)}),function(t,n){let r,i;for(r=0;rbi/3&&P(e)>bi)throw new Error(o+"contains a string greater than "+bi+" utf8 bytes "+pt(a)+" ('"+e.substring(0,50)+"...')");if(e&&"object"==typeof e){let i=!1,s=!1;if(be(e,(e,t)=>{if(".value"===e)i=!0;else if(".priority"!==e&&".sv"!==e&&(s=!0,!Ii(e)))throw new Error(o+" contains an invalid key ("+e+") "+pt(a)+'. Keys must be non-empty strings and can\'t contain ".", "#", "$", "/", "[", or "]"');var n,r;n=a,e=e,0ct(e,t))}function Ai(e,t,n){xi(e,n),Oi(e,e=>ut(e,t)||ut(t,e))}function Oi(e,t){e.recursionDepth_++;let n=!0;for(let i=0;i{Bi(o,e,t,n,r)},o.authTokenProvider_,o.appCheckProvider_),setTimeout(()=>ji(o,!0),0);else{if(null!=t){if("object"!=typeof t)throw new Error("Only objects are supported for option databaseAuthVariableOverride");try{v(t)}catch(e){throw new Error("Invalid authOverride provided: "+e)}}o.persistentConnection_=new gt(o.repoInfo_,e,(e,t,n,r)=>{Bi(o,e,t,n,r)},e=>{ji(o,e)},e=>{var n;n=o,be(e,(e,t)=>{Vi(n,e,t)})},o.authTokenProvider_,o.appCheckProvider_,t),o.server_=o.persistentConnection_}var n;o.authTokenProvider_.addTokenChangeListener(e=>{o.server_.refreshAuthToken(e)}),o.appCheckProvider_.addTokenChangeListener(e=>{o.server_.refreshAppCheckToken(e.token)}),o.statsReporter_=(e=o.repoInfo_,t=()=>new fn(o.stats_,o.server_),n=e.toString(),Ue[n]||(Ue[n]=t()),Ue[n]),o.infoData_=new cn,o.infoSyncTree_=new Dr({startListening:(e,t,n,r)=>{let i=[];const s=o.infoData_.getNode(e._path);return s.isEmpty()||(i=Mr(o.infoSyncTree_,e._path,s),setTimeout(()=>{r("ok")},0)),i},stopListening:()=>{}}),Vi(o,"connected",!1),o.serverSyncTree_=new Dr({startListening:(r,e,t,i)=>(o.server_.listen(r,t,e,(e,t)=>{var n=i(e,t);Ai(o.eventQueue_,r._path,n)}),[]),stopListening:(e,t)=>{o.server_.unlisten(e,t)}})}function Wi(e){const t=e.infoData_.getNode(new Xe(".info/serverTimeOffset"));var n=t.val()||0;return(new Date).getTime()+n}function Ui(e){return Zr({timestamp:Wi(e)})}function Bi(e,t,n,r,i){e.dataUpdateCount++;var s,o,a=new Xe(t);n=e.interceptServerDataCallback_?e.interceptServerDataCallback_(t,n):n;let l=[];l=i?r?(s=I(n,e=>Qt(e)),function(e,t,n,r){if(a=Yr(e,r)){var i=Kr(a),s=i.path,o=i.queryId,a=lt(s,t),i=An.fromObject(n);return $r(e,s,new bn(vn(o),a,i))}return[]}(e.serverSyncTree_,a,s,i)):(s=Qt(n),qr(e.serverSyncTree_,a,s,i)):r?(o=I(n,e=>Qt(e)),t=e.serverSyncTree_,i=a,r=o,o=An.fromObject(r),jr(t,new bn(mn(),i,o))):(o=Qt(n),Mr(e.serverSyncTree_,a,o));let h=a;0{var n=ri(e,t,r.serverSyncTree_,i);dn(s,e,n)});let o=[];_n(s,Ze(),(e,t)=>{o=o.concat(Mr(r.serverSyncTree_,e,t));var n=is(r,e);es(r,n)}),r.onDisconnect_=un(),Ai(r.eventQueue_,Ze(),o)}(e)}function Vi(e,t,n){var r=new Xe("/.info/"+t),i=Qt(n);e.infoData_.updateSnapshot(r,i);i=Mr(e.infoSyncTree_,r,i);Ai(e.eventQueue_,r,i)}function zi(e){return e.nextWriteId_++}function Hi(r,i,e,t,s){Gi(r,"set",{path:i.toString(),value:e,priority:t});var n=Ui(r);const o=Qt(e,t);var a=Ur(r.serverSyncTree_,i),n=ii(o,a,n);const l=zi(r);n=Ar(r.serverSyncTree_,i,n,l,!0);xi(r.eventQueue_,n),r.server_.put(i.toString(),o.val(!0),(e,t)=>{var n="ok"===e;n||ge("set at "+i+" failed: "+e);n=Lr(r.serverSyncTree_,l,!n);Ai(r.eventQueue_,i,n),Ji(0,s,e,t)});n=is(r,i);es(r,n),Ai(r.eventQueue_,n,[])}function Qi(n,r,i){n.server_.onDisconnectCancel(r.toString(),(e,t)=>{"ok"===e&&!function e(n,t){if(at(t))return n.value=null,n.children.clear(),!0;if(null!==n.value){if(n.value.isLeafNode())return!1;{const i=n.value;return n.value=null,i.forEachChild(Ot,(e,t)=>{dn(n,new Xe(e),t)}),e(n,t)}}if(0{"ok"===e&&dn(n.onDisconnect_,r,s),Ji(0,i,e,t)})}function Ki(e,t,n){let r;r=".info"===et(t._path)?Fr(e.infoSyncTree_,t,n):Fr(e.serverSyncTree_,t,n),Di(e.eventQueue_,t._path,r)}function $i(e){e.persistentConnection_&&e.persistentConnection_.interrupt(Li)}function Gi(e,...t){let n="";e.persistentConnection_&&(n=e.persistentConnection_.id+":"),pe(n,...t)}function Ji(e,r,i,s){r&&Pe(()=>{if("ok"===i)r(null);else{var t=(i||"error").toUpperCase();let e=t;s&&(e+=": "+s);const n=new Error(e);n.code=t,r(n)}})}function Xi(e,t,n){return Ur(e.serverSyncTree_,t,n)||jt.EMPTY_NODE}function Zi(t,e=t.transactionQueueTree_){if(e||rs(t,e),li(e)){const n=ns(t,e);p(00===e.status)&&function(i,s,o){const e=o.map(e=>e.currentWriteId),t=Xi(i,s,e);let n=t;var r=t.hash();for(let c=0;c{Gi(i,"transaction put response",{path:h.toString(),status:t});let n=[];if("ok"===t){const r=[];for(let e=0;eo[e].onComplete(null,!0,o[e].currentOutputSnapshotResolved)),o[e].unwatcher();rs(i,ai(i.transactionQueueTree_,s)),Zi(i,i.transactionQueueTree_),Ai(i.eventQueue_,s,n);for(let t=0;t{Zi(t,e)})}function es(e,t){var n=ts(e,t),r=di(n);return function(i,s,o){if(0!==s.length){const h=[];let n=[];const t=s.filter(e=>0===e.status),c=t.map(e=>e.currentWriteId);for(let r=0;r=Mi)e=!0,t="maxretry",n=n.concat(Lr(i.serverSyncTree_,u.currentWriteId,!0));else{const d=Xi(i,u.path,c);u.currentInputSnapshot=d;var l=s[r].update(d.val());if(void 0!==l){ki("transaction failed: Data returned ",l,u.path);let e=Qt(l);"object"==typeof l&&null!=l&&w(l,".priority")||(e=e.updatePriority(d.getPriority()));a=u.currentWriteId,l=Ui(i),l=ii(e,d,l);u.currentOutputSnapshotRaw=e,u.currentOutputSnapshotResolved=l,u.currentWriteId=zi(i),c.splice(c.indexOf(a),1),n=n.concat(Ar(i.serverSyncTree_,u.path,l,u.currentWriteId,u.applyLocally)),n=n.concat(Lr(i.serverSyncTree_,a,!0))}else e=!0,t="nodata",n=n.concat(Lr(i.serverSyncTree_,u.currentWriteId,!0))}Ai(i.eventQueue_,o,n),n=[],e&&(s[r].status=2,function(e){setTimeout(e,Math.floor(0))}(s[r].unwatcher),s[r].onComplete&&("nodata"===t?h.push(()=>s[r].onComplete(null,!1,s[r].currentInputSnapshot)):h.push(()=>s[r].onComplete(new Error(t),!1,null))))}rs(i,i.transactionQueueTree_);for(let e=0;e{t(n,e,r)})}(e,t,n),n.sort((e,t)=>e.order-t.order),n}function rs(t,n){const r=li(n);if(r){let e=0;for(let t=0;t{rs(t,e)})}function is(t,e){var n=di(ts(t,e)),r=ai(t.transactionQueueTree_,e);return function(e,t,n){let r=n?e:e.parent;for(;null!==r;){if(t(r))return;r=r.parent}}(r,e=>{ss(t,e)}),ss(t,r),function t(e,n,r,i){r&&!i&&n(e),ui(e,e=>{t(e,n,!0,i)}),r&&i&&n(e)}(r,e=>{ss(t,e)}),n}function ss(i,s){const o=li(s);if(o){const a=[];let e=[],t=-1;for(let n=0;n.firebaseio.com instead"),r&&"undefined"!==r||"localhost"===n.domain||fe("Cannot parse Firebase url. Please use https://.firebaseio.com"),n.secure||me();var i="ws"===n.scheme||"wss"===n.scheme;return{repoInfo:new Me(n.host,n.secure,r,i,t,"",r!==n.subdomain),path:new Xe(n.pathString)}},as=function(r){let i="",s="",o="",a="",l="",h=!0,c="https",u=443;if("string"==typeof r){let e=r.indexOf("//");0<=e&&(c=r.substring(0,e-1),r=r.substring(e+2));let t=r.indexOf("/");-1===t&&(t=r.length);let n=r.indexOf("?");-1===n&&(n=r.length),i=r.substring(0,Math.min(t,n)),t{})),e.promise}remove(){yi("OnDisconnect.remove",this._path);const e=new _;return Yi(this._repo,this._path,null,e.wrapCallback(()=>{})),e.promise}set(e){yi("OnDisconnect.set",this._path),pi("OnDisconnect.set",e,this._path,!1);const t=new _;return Yi(this._repo,this._path,e,t.wrapCallback(()=>{})),t.promise}setWithPriority(e,t){yi("OnDisconnect.setWithPriority",this._path),pi("OnDisconnect.setWithPriority",e,this._path,!1),gi("OnDisconnect.setWithPriority",t,!1);const n=new _;return function(n,r,e,t,i){const s=Qt(e,t);n.server_.onDisconnectPut(r.toString(),s.val(!0),(e,t)=>{"ok"===e&&dn(n.onDisconnect_,r,s),Ji(0,i,e,t)})}(this._repo,this._path,e,t,n.wrapCallback(()=>{})),n.promise}update(e){yi("OnDisconnect.update",this._path),fi("OnDisconnect.update",e,this._path,!1);const t=new _;return function(r,i,n,s){if(b(n))return pe("onDisconnect().update() called with empty data. Don't do anything."),Ji(0,s,"ok",void 0);r.server_.onDisconnectMerge(i.toString(),n,(e,t)=>{"ok"===e&&be(n,(e,t)=>{var n=Qt(t);dn(r.onDisconnect_,ot(i,e),n)}),Ji(0,s,e,t)})}(this._repo,this._path,e,t.wrapCallback(()=>{})),t.promise}}class ps{constructor(e,t,n,r){this._repo=e,this._path=t,this._queryParams=n,this._orderByCalled=r}get key(){return at(this._path)?null:rt(this._path)}get ref(){return new vs(this._repo,this._path)}get _queryIdentifier(){var e=ln(this._queryParams),e=ue(e);return"{}"===e?"default":e}get _queryObject(){return ln(this._queryParams)}isEqual(e){if(!((e=R(e))instanceof ps))return!1;var t=this._repo===e._repo,n=ct(this._path,e._path),r=this._queryIdentifier===e._queryIdentifier;return t&&n&&r}toJSON(){return this.toString()}toString(){return this._repo.toString()+function(e){let t="";for(let n=e.pieceNum_;nn(new ys(t,bs(this.ref,e),Ot)))}hasChild(e){var t=new Xe(e);return!this._node.getChild(t).isEmpty()}hasChildren(){return!this._node.isLeafNode()&&!this._node.isEmpty()}toJSON(){return this.exportVal()}val(){return this._node.val()}}function ws(e,t){return(e=R(e))._checkNotDeleted("ref"),void 0!==t?bs(e._root,t):e._root}function Cs(e,t){(e=R(e))._checkNotDeleted("refFromURL");const n=os(t,e._repo.repoInfo_.nodeAdmin);Pi("refFromURL",n);var r=n.repoInfo;return e._repo.repoInfo_.isCustomHost()||r.host===e._repo.repoInfo_.host||fe("refFromURL: Host name does not match the current database: (found "+r.host+" but expected "+e._repo.repoInfo_.host+")"),ws(e,n.path.toString())}function bs(e,t){return(null===et((e=R(e))._path)?vi:Ni)("child","path",t,!1),new vs(e._repo,ot(e._path,t))}function Is(e,t){e=R(e),yi("set",e._path),pi("set",t,e._path,!1);const n=new _;return Hi(e._repo,e._path,t,null,n.wrapCallback(()=>{})),n.promise}function Ts(e,t){fi("update",t,e._path,!1);const n=new _;return function(i,s,e,o){Gi(i,"update",{path:s.toString(),value:e});let n=!0;const r=Ui(i),a={};if(be(e,(e,t)=>{n=!1,a[e]=ri(ot(s,e),Qt(t),i.serverSyncTree_,r)}),n)pe("update() called with empty data. Don't do anything."),Ji(0,o,"ok",void 0);else{const l=zi(i);var t=Or(i.serverSyncTree_,s,a,l);xi(i.eventQueue_,t),i.server_.merge(s.toString(),e,(e,t)=>{var n="ok"===e;n||ge("update at "+s+" failed: "+e);var r=Lr(i.serverSyncTree_,l,!n),n=0{var t=is(i,ot(s,e));es(i,t)}),Ai(i.eventQueue_,s,[])}}(e._repo,e._path,t,n.wrapCallback(()=>{})),n.promise}function Es(t){t=R(t);var i,s,o,e=new ds(()=>{}),e=new Ss(e);return i=t._repo,s=t,o=e,(null!=(e=Br(i.serverSyncTree_,s))?Promise.resolve(e):i.server_.get(s).then(e=>{var t,n=Qt(e).withIndex(s._queryParams.getIndex());Wr(i.serverSyncTree_,s,o,!0);let r;return r=s._queryParams.loadsAllData()?Mr(i.serverSyncTree_,s._path,n):(t=Hr(i.serverSyncTree_,s),qr(i.serverSyncTree_,s._path,n,t)),Ai(i.eventQueue_,s._path,r),Fr(i.serverSyncTree_,s,o,null,!0),n},e=>(Gi(i,"get for query "+v(s)+" failed: "+e),Promise.reject(new Error(e))))).then(e=>new ys(e,new vs(t._repo,t._path),t._queryParams.getIndex()))}class Ss{constructor(e){this.callbackContext=e}respondsTo(e){return"value"===e}createEvent(e,t){var n=t._queryParams.getIndex();return new cs("value",this,new ys(e.snapshotNode,new vs(t._repo,t._path),n))}getEventRunner(e){return"cancel"===e.getEventType()?()=>this.callbackContext.onCancel(e.error):()=>this.callbackContext.onValue(e.snapshot,null)}createCancelEvent(e,t){return this.callbackContext.hasCancelCallback?new us(this,e,t):null}matches(e){return e instanceof Ss&&(!e.callbackContext||!this.callbackContext||e.callbackContext.matches(this.callbackContext))}hasAnyCallback(){return null!==this.callbackContext}}class ks{constructor(e,t){this.eventType=e,this.callbackContext=t}respondsTo(e){let t="children_added"===e?"child_added":e;return t="children_removed"===t?"child_removed":t,this.eventType===t}createCancelEvent(e,t){return this.callbackContext.hasCancelCallback?new us(this,e,t):null}createEvent(e,t){p(null!=e.childName,"Child events should have a childName.");var n=bs(new vs(t._repo,t._path),e.childName),r=t._queryParams.getIndex();return new cs(e.type,this,new ys(e.snapshotNode,n,r),e.prevName)}getEventRunner(e){return"cancel"===e.getEventType()?()=>this.callbackContext.onCancel(e.error):()=>this.callbackContext.onValue(e.snapshot,e.prevName)}matches(e){return e instanceof ks&&(this.eventType===e.eventType&&(!this.callbackContext||!e.callbackContext||this.callbackContext.matches(e.callbackContext)))}hasAnyCallback(){return!!this.callbackContext}}function Ns(n,e,t,r,i){let s;if("object"==typeof r&&(s=void 0,i=r),"function"==typeof r&&(s=r),i&&i.onlyOnce){const l=t;var o=(e,t)=>{Ki(n._repo,n,a),l(e,t)};o.userCallback=t.userCallback,o.context=t.context,t=o}o=new ds(t,s||void 0);const a="value"===e?new Ss(o):new ks(e,o);return function(e,t,n){let r;r=".info"===et(t._path)?Wr(e.infoSyncTree_,t,n):Wr(e.serverSyncTree_,t,n),Di(e.eventQueue_,t._path,r)}(n._repo,n,a),()=>Ki(n._repo,n,a)}function Ps(e,t,n,r){return Ns(e,"value",t,n,r)}function Rs(e,t,n,r){return Ns(e,"child_added",t,n,r)}function xs(e,t,n,r){return Ns(e,"child_changed",t,n,r)}function Ds(e,t,n,r){return Ns(e,"child_moved",t,n,r)}function As(e,t,n,r){return Ns(e,"child_removed",t,n,r)}function Os(e,t,n){let r=null;var i=n?new ds(n):null;"value"===t?r=new Ss(i):t&&(r=new ks(t,i)),Ki(e._repo,e,r)}class Ls{}class Ms extends Ls{constructor(e,t){super(),this._value=e,this._key=t}_apply(e){pi("endAt",this._value,e._path,!0);var t=sn(e._queryParams,this._value,this._key);if(ms(t),gs(t),e._queryParams.hasEnd())throw new Error("endAt: Starting point was already set (by another call to endAt, endBefore or equalTo).");return new ps(e._repo,e._path,t,e._orderByCalled)}}class Fs extends Ls{constructor(e,t){super(),this._value=e,this._key=t}_apply(e){pi("endBefore",this._value,e._path,!1);var t=function(e,t,n){let r;return r=e.index_===Ct||n?sn(e,t,n):sn(e,t,ye),r.endBeforeSet_=!0,r}(e._queryParams,this._value,this._key);if(ms(t),gs(t),e._queryParams.hasEnd())throw new Error("endBefore: Starting point was already set (by another call to endAt, endBefore or equalTo).");return new ps(e._repo,e._path,t,e._orderByCalled)}}class qs extends Ls{constructor(e,t){super(),this._value=e,this._key=t}_apply(e){pi("startAt",this._value,e._path,!0);var t=rn(e._queryParams,this._value,this._key);if(ms(t),gs(t),e._queryParams.hasStart())throw new Error("startAt: Starting point was already set (by another call to startAt, startBefore or equalTo).");return new ps(e._repo,e._path,t,e._orderByCalled)}}class Ws extends Ls{constructor(e,t){super(),this._value=e,this._key=t}_apply(e){pi("startAfter",this._value,e._path,!1);var t=function(e,t,n){let r;return r=e.index_===Ct||n?rn(e,t,n):rn(e,t,we),r.startAfterSet_=!0,r}(e._queryParams,this._value,this._key);if(ms(t),gs(t),e._queryParams.hasStart())throw new Error("startAfter: Starting point was already set (by another call to startAt, startAfter, or equalTo).");return new ps(e._repo,e._path,t,e._orderByCalled)}}class Us extends Ls{constructor(e){super(),this._limit=e}_apply(e){if(e._queryParams.hasLimit())throw new Error("limitToFirst: Limit was already set (by another call to limitToFirst or limitToLast).");return new ps(e._repo,e._path,function(e,t){const n=e.copy();return n.limitSet_=!0,n.limit_=t,n.viewFrom_="l",n}(e._queryParams,this._limit),e._orderByCalled)}}class Bs extends Ls{constructor(e){super(),this._limit=e}_apply(e){if(e._queryParams.hasLimit())throw new Error("limitToLast: Limit was already set (by another call to limitToFirst or limitToLast).");return new ps(e._repo,e._path,function(e,t){const n=e.copy();return n.limitSet_=!0,n.limit_=t,n.viewFrom_="r",n}(e._queryParams,this._limit),e._orderByCalled)}}class js extends Ls{constructor(e){super(),this._path=e}_apply(e){fs(e,"orderByChild");var t=new Xe(this._path);if(at(t))throw new Error("orderByChild: cannot pass in empty path. Use orderByValue() instead.");t=new Yt(t),t=on(e._queryParams,t);return gs(t),new ps(e._repo,e._path,t,!0)}}class Vs extends Ls{_apply(e){fs(e,"orderByKey");var t=on(e._queryParams,Ct);return gs(t),new ps(e._repo,e._path,t,!0)}}class zs extends Ls{_apply(e){fs(e,"orderByPriority");var t=on(e._queryParams,Ot);return gs(t),new ps(e._repo,e._path,t,!0)}}class Hs extends Ls{_apply(e){fs(e,"orderByValue");var t=on(e._queryParams,Kt);return gs(t),new ps(e._repo,e._path,t,!0)}}class Qs extends Ls{constructor(e,t){super(),this._value=e,this._key=t}_apply(e){if(pi("equalTo",this._value,e._path,!1),e._queryParams.hasStart())throw new Error("equalTo: Starting point was already set (by another call to startAt/startAfter or equalTo).");if(e._queryParams.hasEnd())throw new Error("equalTo: Ending point was already set (by another call to endAt/endBefore or equalTo).");return new Ms(this._value,this._key)._apply(new qs(this._value,this._key)._apply(e))}}function Ys(e,...t){let n=R(e);for(const r of t)n=r._apply(n);return n}G=vs,p(!vr,"__referenceConstructor has already been defined"),vr=G,J=vs,p(!Rr,"__referenceConstructor has already been defined"),Rr=J;const Ks="FIREBASE_DATABASE_EMULATOR_HOST",$s={};let Gs=!1;function Js(e,t,n,r,i){let s=r||e.options.databaseURL;void 0===s&&(e.options.projectId||fe("Can't determine Firebase Database URL. Be sure to include a Project ID when calling firebase.initializeApp()."),pe("Using default host for project ",e.options.projectId),s=`${e.options.projectId}-default-rtdb.firebaseio.com`);let o=os(s,i),a=o.repoInfo,l,h=void 0;"undefined"!=typeof process&&process.env&&(h=process.env[Ks]),h?(l=!0,s=`http://${h}?ns=${a.namespace}`,o=os(s,i),a=o.repoInfo):l=!o.repoInfo.secure;var c=i&&l?new De(De.OWNER):new xe(e.name,e.options,t);Pi("Invalid Firebase Database URL",o),at(o.path)||fe("Database URL must point to the root of a Firebase Database (not including a child path).");c=function(e,t,n,r){let i=$s[t.name];i||(i={},$s[t.name]=i);var s=i[e.toURLString()];s&&fe("Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.");return s=new Fi(e,Gs,n,r),i[e.toURLString()]=s}(a,e,c,new Re(e.name,n));return new Xs(c,e)}class Xs{constructor(e,t){this._repoInternal=e,this.app=t,this.type="database",this._instanceStarted=!1}get _repo(){return this._instanceStarted||(qi(this._repoInternal,this.app.options.appId,this.app.options.databaseAuthVariableOverride),this._instanceStarted=!0),this._repoInternal}get _root(){return this._rootInternal||(this._rootInternal=new vs(this._repo,Ze())),this._rootInternal}_delete(){return null!==this._rootInternal&&(function(e,t){const n=$s[t];n&&n[e.key]===e||fe(`Database ${t}(${e.repoInfo_}) has already been deleted.`),$i(e),delete n[e.key]}(this._repo,this.app.name),this._repoInternal=null,this._rootInternal=null),Promise.resolve()}_checkNotDeleted(e){null===this._rootInternal&&fe("Cannot call "+e+" on a deleted database.")}}function Zs(){Ye.IS_TRANSPORT_INITIALIZED&&ge("Transport has already been initialized. Please call this function before calling ref or setting up a listener")}function eo(){Zs(),Ve.forceDisallow()}function to(){Zs(),Qe.forceDisallow(),Ve.forceAllow()}function no(e,t,n,r={}){(e=R(e))._checkNotDeleted("useEmulator"),e._instanceStarted&&fe("Cannot call useEmulator() after instance has already been initialized.");var i,s=e._repoInternal;let o=void 0;s.repoInfo_.nodeAdmin?(r.mockUserToken&&fe('mockUserToken is not supported by the Admin SDK. For client access with mock users, please use the "firebase" package instead of "firebase-admin".'),o=new De(De.OWNER)):r.mockUserToken&&(i="string"==typeof r.mockUserToken?r.mockUserToken:function(e,t){if(e.uid)throw new Error('The "uid" field is no longer supported by mockUserToken. Please use "sub" instead for Firebase Auth User ID.');var n=t||"demo-project",r=e.iat||0,i=e.sub||e.user_id;if(!i)throw new Error("mockUserToken must contain 'sub' or 'user_id' field!");return i=Object.assign({iss:`https://securetoken.google.com/${n}`,aud:n,iat:r,exp:r+3600,auth_time:r,sub:i,user_id:i,firebase:{sign_in_provider:"custom",identities:{}}},e),[a(JSON.stringify({alg:"none",type:"JWT"})),a(JSON.stringify(i)),""].join(".")}(r.mockUserToken,e.app.options.projectId),o=new De(i)),r=s,e=t,t=n,n=o,r.repoInfo_=new Me(`${e}:${t}`,!1,r.repoInfo_.namespace,r.repoInfo_.webSocketOnly,r.repoInfo_.nodeAdmin,r.repoInfo_.persistenceKey,r.repoInfo_.includeNamespaceInQueryParams,!0),n&&(r.authTokenProvider_=n)}function ro(e){(e=R(e))._checkNotDeleted("goOnline"),(e=e._repo).persistentConnection_&&e.persistentConnection_.resume(Li)}function io(e,t){_e(e,t)}const so={".sv":"timestamp"};class oo{constructor(e,t){this.committed=e,this.snapshot=t}toJSON(){return{committed:this.committed,snapshot:this.snapshot.toJSON()}}}function ao(i,e,t){if(i=R(i),yi("Reference.transaction",i._path),".length"===i.key||".keys"===i.key)throw"Reference.transaction failed: "+i.key+" is a read-only object.";var n=null===(r=null==t?void 0:t.applyLocally)||void 0===r||r;const s=new _;var r=Ps(i,()=>{});return function(t,n,e,r,i,s){Gi(t,"transaction on "+n);const o={path:n,update:e,onComplete:r,status:null,order:re(),applyLocally:s,retryCount:0,unwatcher:i,abortReason:null,currentWriteId:null,currentInputSnapshot:null,currentOutputSnapshotRaw:null,currentOutputSnapshotResolved:null},a=Xi(t,n,void 0);o.currentInputSnapshot=a;var l=o.update(a.val());if(void 0===l)o.unwatcher(),o.currentOutputSnapshotRaw=null,o.currentOutputSnapshotResolved=null,o.onComplete&&o.onComplete(null,!1,o.currentInputSnapshot);else{ki("transaction failed: Data returned ",l,o.path),o.status=0;var h=ai(t.transactionQueueTree_,n);const c=li(h)||[];c.push(o),hi(h,c);let e;if("object"==typeof l&&null!==l&&w(l,".priority"))e=C(l,".priority"),p(Si(e),"Invalid priority returned by transaction. Priority must be a valid string, finite number, server value, or null.");else{const u=Ur(t.serverSyncTree_,n)||jt.EMPTY_NODE;e=u.getPriority().val()}h=Ui(t),l=Qt(l,e),h=ii(l,a,h);o.currentOutputSnapshotRaw=l,o.currentOutputSnapshotResolved=h,o.currentWriteId=zi(t);h=Ar(t.serverSyncTree_,n,h,o.currentWriteId,o.applyLocally);Ai(t.eventQueue_,n,h),Zi(t,t.transactionQueueTree_)}}(i._repo,i._path,e,(e,t,n)=>{var r;e?s.reject(e):(r=new ys(n,new vs(i._repo,i._path),Ot),s.resolve(new oo(t,r)))},r,n),s.promise}gt.prototype.simpleListen=function(e,t){this.sendRequest("q",{p:e},t)},gt.prototype.echo=function(e,t){this.sendRequest("echo",{d:e},t)},j(Co.SDK_VERSION),Co._registerComponent(new x("database",(e,{instanceIdentifier:t})=>{return Js(e.getProvider("app").getImmediate(),e.getProvider("auth-internal"),e.getProvider("app-check-internal"),t)},"PUBLIC").setMultipleInstances(!0)),Co.registerVersion(U,"0.14.4",X),Co.registerVersion(U,"0.14.4","esm2017");function lo(e){var t="FIREBASE WARNING: "+e;ho.warn(t)}const ho=new W("@firebase/database-compat");class co{constructor(e){this._delegate=e}cancel(t){E("OnDisconnect.cancel",0,1,arguments.length),k("OnDisconnect.cancel","onComplete",t,!0);const e=this._delegate.cancel();return t&&e.then(()=>t(null),e=>t(e)),e}remove(t){E("OnDisconnect.remove",0,1,arguments.length),k("OnDisconnect.remove","onComplete",t,!0);const e=this._delegate.remove();return t&&e.then(()=>t(null),e=>t(e)),e}set(e,t){E("OnDisconnect.set",1,2,arguments.length),k("OnDisconnect.set","onComplete",t,!0);const n=this._delegate.set(e);return t&&n.then(()=>t(null),e=>t(e)),n}setWithPriority(e,t,n){E("OnDisconnect.setWithPriority",2,3,arguments.length),k("OnDisconnect.setWithPriority","onComplete",n,!0);const r=this._delegate.setWithPriority(e,t);return n&&r.then(()=>n(null),e=>n(e)),r}update(t,n){if(E("OnDisconnect.update",1,2,arguments.length),Array.isArray(t)){const r={};for(let e=0;en(null),e=>n(e)),e}}class uo{constructor(e,t){this.committed=e,this.snapshot=t}toJSON(){return E("TransactionResult.toJSON",0,1,arguments.length),{committed:this.committed,snapshot:this.snapshot.toJSON()}}}class _o{constructor(e,t){this._database=e,this._delegate=t}val(){return E("DataSnapshot.val",0,0,arguments.length),this._delegate.val()}exportVal(){return E("DataSnapshot.exportVal",0,0,arguments.length),this._delegate.exportVal()}toJSON(){return E("DataSnapshot.toJSON",0,1,arguments.length),this._delegate.toJSON()}exists(){return E("DataSnapshot.exists",0,0,arguments.length),this._delegate.exists()}child(e){return E("DataSnapshot.child",0,1,arguments.length),e=String(e),Ni("DataSnapshot.child","path",e,!1),new _o(this._database,this._delegate.child(e))}hasChild(e){return E("DataSnapshot.hasChild",1,1,arguments.length),Ni("DataSnapshot.hasChild","path",e,!1),this._delegate.hasChild(e)}getPriority(){return E("DataSnapshot.getPriority",0,0,arguments.length),this._delegate.priority}forEach(t){return E("DataSnapshot.forEach",1,1,arguments.length),k("DataSnapshot.forEach","action",t,!1),this._delegate.forEach(e=>t(new _o(this._database,e)))}hasChildren(){return E("DataSnapshot.hasChildren",0,0,arguments.length),this._delegate.hasChildren()}get key(){return this._delegate.key}numChildren(){return E("DataSnapshot.numChildren",0,0,arguments.length),this._delegate.size}getRef(){return E("DataSnapshot.ref",0,0,arguments.length),new fo(this._database,this._delegate.ref)}get ref(){return this.getRef()}}class po{constructor(e,t){this.database=e,this._delegate=t}on(e,n,t,r){var i;E("Query.on",2,4,arguments.length),k("Query.on","callback",n,!1);const s=po.getCancelAndContextArgs_("Query.on",t,r);var o=(e,t)=>{n.call(s.context,new _o(this.database,e),t)};o.userCallback=n,o.context=s.context;var a=null===(i=s.cancel)||void 0===i?void 0:i.bind(s.context);switch(e){case"value":return Ps(this._delegate,o,a),n;case"child_added":return Rs(this._delegate,o,a),n;case"child_removed":return As(this._delegate,o,a),n;case"child_changed":return xs(this._delegate,o,a),n;case"child_moved":return Ds(this._delegate,o,a),n;default:throw new Error(S("Query.on","eventType")+'must be a valid event type = "value", "child_added", "child_removed", "child_changed", or "child_moved".')}}off(e,t,n){var r;E("Query.off",0,3,arguments.length),function(e,t,n){if(!n||void 0!==t)switch(t){case"value":case"child_added":case"child_removed":case"child_changed":case"child_moved":break;default:throw new Error(S(e,"eventType")+'must be a valid event type = "value", "child_added", "child_removed", "child_changed", or "child_moved".')}}("Query.off",e,!0),k("Query.off","callback",t,!0),N("Query.off","context",n,!0),t?((r=()=>{}).userCallback=t,r.context=n,Os(this._delegate,e,r)):Os(this._delegate,e)}get(){return Es(this._delegate).then(e=>new _o(this.database,e))}once(e,r,t,n){E("Query.once",1,4,arguments.length),k("Query.once","callback",r,!0);const i=po.getCancelAndContextArgs_("Query.once",t,n),s=new _;var o=(e,t)=>{var n=new _o(this.database,e);r&&r.call(i.context,n,t),s.resolve(n)};o.userCallback=r,o.context=i.context;var a=e=>{i.cancel&&i.cancel.call(i.context,e),s.reject(e)};switch(e){case"value":Ps(this._delegate,o,a,{onlyOnce:!0});break;case"child_added":Rs(this._delegate,o,a,{onlyOnce:!0});break;case"child_removed":As(this._delegate,o,a,{onlyOnce:!0});break;case"child_changed":xs(this._delegate,o,a,{onlyOnce:!0});break;case"child_moved":Ds(this._delegate,o,a,{onlyOnce:!0});break;default:throw new Error(S("Query.once","eventType")+'must be a valid event type = "value", "child_added", "child_removed", "child_changed", or "child_moved".')}return s.promise}limitToFirst(e){return E("Query.limitToFirst",1,1,arguments.length),new po(this.database,Ys(this._delegate,function(e){if("number"!=typeof e||Math.floor(e)!==e||e<=0)throw new Error("limitToFirst: First argument must be a positive integer.");return new Us(e)}(e)))}limitToLast(e){return E("Query.limitToLast",1,1,arguments.length),new po(this.database,Ys(this._delegate,function(e){if("number"!=typeof e||Math.floor(e)!==e||e<=0)throw new Error("limitToLast: First argument must be a positive integer.");return new Bs(e)}(e)))}orderByChild(e){return E("Query.orderByChild",1,1,arguments.length),new po(this.database,Ys(this._delegate,function(e){if("$key"===e)throw new Error('orderByChild: "$key" is invalid. Use orderByKey() instead.');if("$priority"===e)throw new Error('orderByChild: "$priority" is invalid. Use orderByPriority() instead.');if("$value"===e)throw new Error('orderByChild: "$value" is invalid. Use orderByValue() instead.');return Ni("orderByChild","path",e,!1),new js(e)}(e)))}orderByKey(){return E("Query.orderByKey",0,0,arguments.length),new po(this.database,Ys(this._delegate,new Vs))}orderByPriority(){return E("Query.orderByPriority",0,0,arguments.length),new po(this.database,Ys(this._delegate,new zs))}orderByValue(){return E("Query.orderByValue",0,0,arguments.length),new po(this.database,Ys(this._delegate,new Hs))}startAt(e=null,t){return E("Query.startAt",0,2,arguments.length),new po(this.database,Ys(this._delegate,([e=null,t]=[e,t],mi("startAt","key",t,!0),new qs(e,t))))}startAfter(e=null,t){return E("Query.startAfter",0,2,arguments.length),new po(this.database,Ys(this._delegate,(e=e,t=t,mi("startAfter","key",t,!0),new Ws(e,t))))}endAt(e=null,t){return E("Query.endAt",0,2,arguments.length),new po(this.database,Ys(this._delegate,(e=e,t=t,mi("endAt","key",t,!0),new Ms(e,t))))}endBefore(e=null,t){return E("Query.endBefore",0,2,arguments.length),new po(this.database,Ys(this._delegate,(e=e,t=t,mi("endBefore","key",t,!0),new Fs(e,t))))}equalTo(e,t){return E("Query.equalTo",1,2,arguments.length),new po(this.database,Ys(this._delegate,(e=e,t=t,mi("equalTo","key",t,!0),new Qs(e,t))))}toString(){return E("Query.toString",0,0,arguments.length),this._delegate.toString()}toJSON(){return E("Query.toJSON",0,1,arguments.length),this._delegate.toJSON()}isEqual(e){if(E("Query.isEqual",1,1,arguments.length),e instanceof po)return this._delegate.isEqual(e._delegate);throw new Error("Query.isEqual failed: First argument must be an instance of firebase.database.Query.")}static getCancelAndContextArgs_(e,t,n){const r={cancel:void 0,context:void 0};if(t&&n)r.cancel=t,k(e,"cancel",r.cancel,!0),r.context=n,N(e,"context",r.context,!0);else if(t)if("object"==typeof t&&null!==t)r.context=t;else{if("function"!=typeof t)throw new Error(S(e,"cancelOrContext")+" must either be a cancel callback or a context object.");r.cancel=t}return r}get ref(){return new fo(this.database,new vs(this._delegate._repo,this._delegate._path))}}class fo extends po{constructor(e,t){super(e,new ps(t._repo,t._path,new nn,!1)),this.database=e,this._delegate=t}getKey(){return E("Reference.key",0,0,arguments.length),this._delegate.key}child(e){return E("Reference.child",1,1,arguments.length),"number"==typeof e&&(e=String(e)),new fo(this.database,bs(this._delegate,e))}getParent(){E("Reference.parent",0,0,arguments.length);var e=this._delegate.parent;return e?new fo(this.database,e):null}getRoot(){return E("Reference.root",0,0,arguments.length),new fo(this.database,this._delegate.root)}set(e,t){E("Reference.set",1,2,arguments.length),k("Reference.set","onComplete",t,!0);const n=Is(this._delegate,e);return t&&n.then(()=>t(null),e=>t(e)),n}update(t,n){if(E("Reference.update",1,2,arguments.length),Array.isArray(t)){const r={};for(let e=0;en(null),e=>n(e)),e}setWithPriority(e,t,n){E("Reference.setWithPriority",2,3,arguments.length),k("Reference.setWithPriority","onComplete",n,!0);const r=function(e,t,n){if(yi("setWithPriority",e._path),pi("setWithPriority",t,e._path,!1),gi("setWithPriority",n,!1),".length"===e.key||".keys"===e.key)throw"setWithPriority failed: "+e.key+" is a read-only object.";const r=new _;return Hi(e._repo,e._path,t,n,r.wrapCallback(()=>{})),r.promise}(this._delegate,e,t);return n&&r.then(()=>n(null),e=>n(e)),r}remove(t){E("Reference.remove",0,1,arguments.length),k("Reference.remove","onComplete",t,!0);const e=(n=this._delegate,yi("remove",n._path),Is(n,null));var n;return t&&e.then(()=>t(null),e=>t(e)),e}transaction(e,t,n){E("Reference.transaction",1,3,arguments.length),k("Reference.transaction","transactionUpdate",e,!1),k("Reference.transaction","onComplete",t,!0),function(e,t,n,r){if((!r||void 0!==n)&&"boolean"!=typeof n)throw new Error(S(e,t)+"must be a boolean.")}("Reference.transaction","applyLocally",n,!0);const r=ao(this._delegate,e,{applyLocally:n}).then(e=>new uo(e.committed,new _o(this.database,e.snapshot)));return t&&r.then(e=>t(null,e.committed,e.snapshot),e=>t(e,!1,null)),r}setPriority(e,t){E("Reference.setPriority",1,2,arguments.length),k("Reference.setPriority","onComplete",t,!0);const n=function(e,t){e=R(e),yi("setPriority",e._path),gi("setPriority",t,!1);const n=new _;return Hi(e._repo,ot(e._path,".priority"),t,null,n.wrapCallback(()=>{})),n.promise}(this._delegate,e);return t&&n.then(()=>t(null),e=>t(e)),n}push(e,t){E("Reference.push",0,2,arguments.length),k("Reference.push","onComplete",t,!0);const n=function(e,t){e=R(e),yi("push",e._path),pi("push",t,e._path,!0);var n=Wi(e._repo),n=hs(n);const r=bs(e,n),i=bs(e,n);let s;return s=null!=t?Is(i,t).then(()=>i):Promise.resolve(i),r.then=s.then.bind(s),r.catch=s.then.bind(s,void 0),r}(this._delegate,e),r=n.then(e=>new fo(this.database,e));t&&r.then(()=>t(null),e=>t(e));const i=new fo(this.database,n);return i.then=r.then.bind(r),i.catch=r.catch.bind(r,void 0),i}onDisconnect(){return yi("Reference.onDisconnect",this._delegate._path),new co(new _s(this._delegate._repo,this._delegate._path))}get key(){return this.getKey()}get parent(){return this.getParent()}get root(){return this.getRoot()}}class go{constructor(e,t){this._delegate=e,this.app=t,this.INTERNAL={delete:()=>this._delegate._delete(),forceWebSockets:eo,forceLongPolling:to}}useEmulator(e,t,n={}){no(this._delegate,e,t,n)}ref(e){if(E("database.ref",0,1,arguments.length),e instanceof fo){var t=Cs(this._delegate,e.toString());return new fo(this,t)}t=ws(this._delegate,e);return new fo(this,t)}refFromURL(e){E("database.refFromURL",1,1,arguments.length);var t=Cs(this._delegate,e);return new fo(this,t)}goOffline(){var e;E("database.goOffline",0,0,arguments.length),(e=R(e=this._delegate))._checkNotDeleted("goOffline"),$i(e._repo)}goOnline(){return E("database.goOnline",0,0,arguments.length),ro(this._delegate)}}go.ServerValue={TIMESTAMP:so,increment:e=>({".sv":{increment:e}})};var mo,vo=Object.freeze({__proto__:null,initStandalone:function({app:e,url:t,version:n,customAuthImpl:r,namespace:i,nodeAdmin:s=!1}){j(n);const o=new A("auth-internal",new O("database-standalone"));return o.setComponent(new x("auth-internal",()=>r,"PRIVATE")),{instance:new go(Js(e,o,void 0,t,s),e),namespace:i}}});const yo=go.ServerValue;(mo=t.default).INTERNAL.registerComponent(new x("database-compat",(e,{instanceIdentifier:t})=>{var n=e.getProvider("app-compat").getImmediate(),r=e.getProvider("database").getImmediate({identifier:t});return new go(r,n)},"PUBLIC").setServiceProps({Reference:fo,Query:po,Database:go,DataSnapshot:_o,enableLogging:io,INTERNAL:vo,ServerValue:yo}).setMultipleInstances(!0)),mo.registerVersion("@firebase/database-compat","0.3.4")}).apply(this,arguments)}catch(e){throw console.error(e),new Error("Cannot instantiate firebase-database-compat.js - be sure to load firebase-app.js first.")}}); //# sourceMappingURL=firebase-database-compat.js.map